How to create index in SharePoint online list using CSOM .Net managed object model code?

How to create index in SharePoint online list using CSOM .Net managed object model code?

In this post, we will discuss how we can create an index in SharePoint online list using csom.Net managed object model code. Here we will do this inside a console application and we will try to connect to SharePoint online site.

To work with.Net managed object model code we need to add below two dlls:
– Microsoft.SharePoint.Client.dll
– Microsoft.SharePoint.Client.Runtime.dll

SharePoint deveopment training course

You can also see an article on:

How to create a console application to work with SharePoint Online Office 365 sites?

Here I have a list name as SourceList and I want to do indexing in below two columns:
– Title
– EmailID

Indexing in a column in a list helps in increasing the performance when we use filters. This also helps to overcome the listview threshold issue.

We can index upto 20 columns per list or library. We can not index in all type of columns. Below are the supported and unsupported column types:

Also read:

Step by step create a web api and deploy in Microsoft Azure for SharePoint online using visual studio 2015

SharePoint online Read csv file from document library using .Net managed client object model csom

Supported Column Types:

Single line of text
Choice (single value)
Number
Currency
Date and Time
Person or Group (single value)
Managed Metadata
Yes/No
Lookup

Unsupported Column Types:

Multiple lines of text
Choice (multi-valued)
Calculated
Hyperlink or Picture
Custom Columns
Person or Group (multi-valued)
External data

Below is the full code to do indexing in a list.

public static void CreateIndexForList(string siteURL, string listName)

{

using (ClientContext ctx = new ClientContext(siteURL))

{

ctx.AuthenticationMode = ClientAuthenticationMode.Default;

ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());

var web = ctx.Web;

ctx.ExecuteQuery();

List list = ctx.Web.Lists.GetByTitle(listName);

ctx.Load(list);

ctx.ExecuteQuery();

string Columns = “Title|EmailID”;

string[] splitCL = Columns.Split(‘|’);

for (int j = 0; j < splitCL.Length; j++)

{

Field field = list.Fields.GetByTitle(splitCL[j].Trim());

field.Indexed = true;

field.Update();

}

ctx.ExecuteQuery();

}

}

private static string GetSPOAccountName()

{

try

{

return ConfigurationManager.AppSettings[“SPOAccount”];

}

catch

{

throw;

}

}

private static SecureString GetSPOSecureStringPassword()

{

try

{

var secureString = new SecureString();

foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])

{

secureString.AppendChar(c);

}

return secureString;

}

catch

{

throw;

}

}

Once you run the code, it will create index in those two columns which you can see from the list settings page-> Index columns like below:

 How to create index in SharePoint online list using CSOM .Net managed object model code?
How to create index in SharePoint online list using CSOM .Net managed object model code?

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →