Share via


Create an index in a list using CSOM

I have been asked about how to create an list index programmatically in SharePoint 2010\2013\2016. You can do that using server object model as follows:

 using (SPSite site = 
 
 new SPSite("https://SHAREPOINT_SITE_URL"))
 
 using (SPWeb web = site.OpenWeb())
 
 {
 
 SPList list = web.Lists["LIST_TITLE"];
 
 SPField field = list.Fields["FIELD_NAME"];
 
 field.Indexed = true;
 
 field.Update();
 
 }
 

For Office 365 or for SharePoint on-premises farms that you can only access remotely you can do it using CSOM as follows:

 ClientContext Cx = 
 new ClientContext("https://SHAREPOINT_SITE_URL");
 
 List L = Cx.Web.Lists.GetByTitle("LIST_TITLE");
 
 Cx.Load(L);
 
 Cx.ExecuteQuery();
 
 Field f = L.Fields.GetByTitle("FIELD_NAME");
 
 f.Indexed = true;
 
 f.Update();
 
 Cx.ExecuteQuery();