How To Add Index On SharePoint Fields Using C#

In this article, we are going to see the steps to create an index to a SharePoint field using C#. Indexing is a data structure which is used to quickly locate and access the data in the database table. The Client-Side Object Model (CSOM) is used internally for these operations.

Why Indexed columns?

  • Indexed columns allow fast retrieval.
  • It improves the performance of the database.
  • It reduces the disc access while performing the query.

Step 1

Open Visual Studio, go to File -> New -> and select Project.

SharePoint

Step 2

In Templates select Visual C#, select Console App (.Net Framework) and giveappropriate name in name text box and then click ok button

 

SharePoint

 

Step 3

  • After installing SharePoint core library, you can see the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll in your project reference.

    SharePoint

     

Step 4

Open the program.cs file and type the below code

Program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.SharePoint.Client;  
  4. namespace SPField_Indexing {  
  5.     class Program {  
  6.         static void Main(string[] args) {  
  7.             try {  
  8.                 SPHelper sphelper = new SPHelper();  
  9.                 string url = "", userName = "", password = "";  
  10.                 Console.WriteLine("Please enter site url:");  
  11.                 url = Console.ReadLine();  
  12.                 Console.WriteLine("Please enter username:");  
  13.                 userName = Console.ReadLine();  
  14.                 Console.WriteLine("Please enter password:");  
  15.                 password = Console.ReadLine();  
  16.                 if (url != null && userName != null && password != null) {  
  17.                     sphelper.doIndexing(url, userName, password);  
  18.                 }  
  19.             } catch (Exception) {  
  20.                 throw;  
  21.             }  
  22.         }  
  23.     }  
  24.     class SPHelper {  
  25.         public void doIndexing(string SiteUrl, string UserName, string Password) {  
  26.             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();  
  27.             using(var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(SiteUrl, UserName, Password)) {  
  28.                 if (clientContext != null) {  
  29.                     //Give list name to select the list  
  30.                     string listName = "HubflyTeam";  
  31.                     //Give the filed names to be indexed  
  32.                     List < string > FieldName = new List < string > () {  
  33.                         "NameHF",  
  34.                         "LocationHF"  
  35.                     };  
  36.                     List list = clientContext.Web.Lists.GetByTitle(listName);  
  37.                     clientContext.Load(list);  
  38.                     clientContext.ExecuteQuery();  
  39.                     foreach(string fields in FieldName) {  
  40.                         Field field = list.Fields.GetByTitle(fields);  
  41.                         field.Indexed = true;  
  42.                         field.Update();  
  43.                         clientContext.ExecuteQuery();  
  44.                     }  
  45.                     Console.WriteLine("SharePoint Field Indexed Successfully!");  
  46.                     Console.ReadLine();  
  47.                 }  
  48.             }  
  49.         }  
  50.     }  
Step 5

Run the project using the F5 function key. Enter the Site URL, Username and Password to access the SharePoint Site,

 

SharePoint

Step 6

Open the list setting like the below Snapshot.

SharePoint

 

Drag down and click Indexed columns like the below snapshot,

SharePoint

That's it, SharePoint Field has been indexed successfully. 

SharePoint

Thus, you have seen steps to create SharePoint field index programmatically using C# code. Feel free to fill up the comment box below if you need any assistance.