Latest Posts

Create View in SharePoint using CSOM

The example in this topic show how to use CSOM to Create View in SharePoint
  • Please follow the steps below to execute the code in Visual Studio using Console Application. You can customize the solution according to your requirements.
  • Check the article to Connect to SharePoint context using CSOM. This is very helpful if you are doing any programming in CSOM.
  • Don't forget to use the assemblies as mentioned in the code.

How to run CSOM code in SharePoint?

  • Open your Visual Studio.
  • From the template, select Console Application as shown in the screenshot
  • Select the .Net Framework version from the top drop-down as well. You can also change the .Net Framework after creating the solution.
  • Enter Project Name, Location and Solution Name and click on OK.
  • create console application using Visual Studio
  • Now your program.cs file will open. Copy the code in main function.
  • Once you are done with your code, just hit F5 or Run the application.

  • using Microsoft.SharePoint.Client;
  • using System.Linq;
  •  
  • using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
  • {
  • // clientcontext.Web.Lists.GetById - This option also can be used to get the list using List GUID
  • // This value is NOT List internal name
  • List targetList = clientcontext.Web.Lists.GetByTitle("List Name");
  •  
  • ViewCollection viewCollection = targetList.Views;
  • clientcontext.Load(viewCollection);
  •  
  • ViewCreationInformation viewCreationInformation = new ViewCreationInformation();
  • viewCreationInformation.Title = "View Name";
  •  
  • // Specify type of the view. Below are the options
  •  
  • // 1. none - The type of the list view is not specified
  •  
  • // 2. html - Sspecifies an HTML list view type
  •  
  • // 3. grid - Specifies a datasheet list view type
  •  
  • // 4. calendar- Specifies a calendar list view type
  •  
  • // 5. recurrence - Specifies a list view type that displays recurring events
  •  
  • // 6. chart - Specifies a chart list view type
  •  
  • // 7. gantt - Specifies a Gantt chart list view type
  •  
  • viewCreationInformation.ViewTypeKind = ViewType.Html;
  •  
  • // You can optionally specify row limit for the view
  • viewCreationInformation.RowLimit = 10;
  •  
  • // You can optionally specify a query as mentioned below.
  • // Create one CAML query to filter list view and mention that query below
  • viewCreationInformation.Query = "<Where><Eq><FieldRef Name = 'Location' /><Value Type = 'Text'>India</Value></Eq></Where>";
  •  
  • // Add all the fields over here with comma separated value as mentioned below
  • // You can mention display name or internal name of the column
  • string CommaSeparateColumnNames = "Column name 1, column Name 2, Column Name 3";
  • viewCreationInformation.ViewFields = CommaSeparateColumnNames.Split(',');
  •  
  • View listView = viewCollection.Add(viewCreationInformation);
  • clientcontext.ExecuteQuery();
  •  
  • // Code to update the display name for the view.
  • listView.Title = "View Display Name";
  •  
  • // You can optionally specify Aggregation: Field references for totals columns or calculated columns
  • listView.Aggregations = "<FieldRef Name='Title' Type='COUNT'/>";
  •  
  • listView.Update();
  • clientcontext.ExecuteQuery();
  • }

Thank you for reading this article. This code was tested in SharePoint 2013

We value your Feedback:

Page URL:

Name:

 

Email:

 
 

Suggestion:

 

© 2024 Code SharePoint