SharePoint 2010 Update Default List View By Code


SharePoint 2010 Create List view using Code. Open visual studio Create a console Application give it name and Add “Microsoft.SharePoint” Ref. to your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;

namespace SharepointForDeveloper_AddView
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Missing Parameters 1. <ListUrl> 2. <XmlFile Path>");
                return;
            }
            string paramListUrl = args[0];
            string paramViewXml = args[1];

            using (SPSite site = new SPSite(paramListUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string serverRelativePath = GetRelativeUrl(paramListUrl);
                    SPList list = web.GetList(serverRelativePath);

                    XmlDocument doc = new XmlDocument();
                    doc.Load(paramViewXml);
                    SPView loadedView = new SPView(list, doc);
                   
                    SPView newView = list.Views.Add(loadedView.Title, loadedView.ViewFields.ToStringCollection(), loadedView.Query, loadedView.RowLimit, loadedView.Paged, loadedView.DefaultView);
                    string viewXmlString = doc.DocumentElement.OuterXml;
                    newView.SetViewXml(viewXmlString);
                    //update the view
                    newView.Update();
                }
            }
        }

        public static string GetRelativeUrl(string listUrl)
        {
            Uri uri = null;
            if (Uri.TryCreate(listUrl, UriKind.Absolute, out uri))
            {
                return (uri.AbsolutePath);
            }
            else throw new ArgumentException("Invalid Url");
        }

    }
}


Add XML file to your project. XML File that contents the following XML. give it name "SHAREPOINTFORDEVELOPER_VIEW.XML" 


<?xml version="1.0" encoding="utf-8" ?>
<View DisplayName="All Items2" DefaultView="TRUE" BaseViewID="1" Type="HTML" MobileView="TRUE" MobileDefaultView="TRUE"
      ImageUrl="/_layouts/images/generic.png" XslLink="main.xsl" WebPartZoneID="Main" WebPartOrder="1"
      Url="AllItems2.aspx" SetupPath="pages\viewpage.aspx">
  <XslLink>main.xsl</XslLink>
  <Query>
    <OrderBy>
      <FieldRef Name="ID" />
    </OrderBy>
  </Query>
  <ViewFields>
    <FieldRef Name="Attachments" />
    <FieldRef Name="LinkTitle" />
  </ViewFields>
  <RowLimit Paged="TRUE">30</RowLimit>
  <Formats>
    <FormatDef Type="RowHeight" Value="Auto" />
    <Format Name="Attachments">
      <FormatDef Type="ColWidth" Value="39" />
    </Format>
    <Format Name="LinkTitle">
      <FormatDef Type="WrapText" Value="1" />
      <FormatDef Type="ColWidth" Value="201" />
    </Format>
  </Formats>
  <Aggregations Value="Off">
    <FieldRef Name="LinkTitle" Type="COUNT" />
  </Aggregations>
</View>




Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Send Email using SharePoint Rest API

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)