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>");
            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();
                    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;
                    //update the view

        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">
      <FieldRef Name="ID" />
    <FieldRef Name="Attachments" />
    <FieldRef Name="LinkTitle" />
  <RowLimit Paged="TRUE">30</RowLimit>
    <FormatDef Type="RowHeight" Value="Auto" />
    <Format Name="Attachments">
      <FormatDef Type="ColWidth" Value="39" />
    <Format Name="LinkTitle">
      <FormatDef Type="WrapText" Value="1" />
      <FormatDef Type="ColWidth" Value="201" />
  <Aggregations Value="Off">
    <FieldRef Name="LinkTitle" Type="COUNT" />

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

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

STS CryptographicException Error : Key set does not exist