Create an index in SharePoint Online list using CSOM

This SharePoint CSOM tutorial explains, how to create an index in SharePoint online list using csom .Net managed object model code.

If you are working with large list, then indexing will be very helpful in SharePoint.

Here I have created a console application and connect to a SharePoint Online site to do this example.

To work with.Net managed object model code we need to add below two dlls:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

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 in a SharePoint list. This also helps to overcome the listview threshold issue which comes when you are to retrieve more than 5000 items from a list at once.

We can index up to 20 columns per list or library. We can not index in all type of columns.

Below are the supported and unsupported column types:

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

Create index in SharePoint Online list using CSOM

Below is the full code to do indexing in a list in SharePoint Online using CSOM.

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 SecureString GetSPOSecureStringPassword()
        {
            try
            {
                var secureString = new SecureString();
                foreach (char c in "MyPassword")
                {
                    secureString.AppendChar(c);
                }
                return secureString;
            }
            catch
            {
                throw;
            }
        }

        private static string GetSPOAccountName()
        {
            try
            {
                return "bijay@<tenantname>@onmicrosoft.com";
            }
            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:

Create index in SharePoint Online list using CSOM

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial we learned, how to create an index in SharePoint online list using CSOM .Net managed object model code.

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

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

  • […] to create an index in SharePoint online list using CSOM .Net managed object model […]

  • >