How to Create a View in SharePoint 2013 using CSOM

Steps

  • Open Visual Studio in your system
  • Select Console Applciation template and give as name .
  • Add a Microsoft.Cleint Assembly refrence file in right side refrence tab in visual studio.
  • Replace Program.cs with the source c 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. namespace CSOMCodeSamples  
  8. namespace CSOMCodeSamples   
  9. {  
  10.     class Program   
  11.     {  
  12.         static void Main(string[] args)   
  13.         {  
  14.             // ClientContext - Get the context for the SharePoint Site    
  15.   
  16.             ClientContext clientContext = new ClientContext("http://gauti.sharepoint.com/sites/sp1/");  
  17.             // Get the SharePoint web    
  18.             Web web = clientContext.Web;  
  19.                 
  20.             List list = web.Lists.GetByTitle("CustomList");    
  21.     
  22.             // Get all the view  
  23.             for the custom list  
  24.             ViewCollection viewColl = list.Views;  
  25.   
  26.             // Specify the columns that should be displayed    
  27.   
  28.             string[] viewFields = {"Column1"};  
  29.   
  30.             // Specifies the properties used to create a new list view    
  31.   
  32.             ViewCreationInformation creationInfo = new ViewCreationInformation();  
  33.             creationInfo.Title = "GVRNewView";  
  34.             creationInfo.RowLimit = 50;  
  35.             creationInfo.ViewFields = viewFields;  
  36.             creationInfo.ViewTypeKind = ViewType.None;  
  37.             creationInfo.SetAsDefaultView = true;  
  38.             viewColl.Add(creationInfo);  
  39.             clientContext.ExecuteQuery();  
  40.         }  
  41.     }  
  42. }  

Hit F5 and check the output.

Thanks for reading my blogs.

Ebook Download
View all
Learn
View all