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.

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

Step 3
We need to install SharePoint PnP Core Library in our C# project to perform SharePoint field indexing operations on SharePoint site. To install the library refer this blog How To Install SharePoint PnP Core Library In Visual Studio 2017
After installing SharePoint core library, you can see the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll in your project reference.

Step 4
Open the program.cs file and type the below code
Program.cs
- using System;
- using System.Collections.Generic;
- using Microsoft.SharePoint.Client;
- namespace SPField_Indexing {
- class Program {
- static void Main(string[] args) {
- try {
- SPHelper sphelper = new SPHelper();
- string url = "", userName = "", password = "";
- Console.WriteLine("Please enter site url:");
- url = Console.ReadLine();
- Console.WriteLine("Please enter username:");
- userName = Console.ReadLine();
- Console.WriteLine("Please enter password:");
- password = Console.ReadLine();
- if (url != null && userName != null && password != null) {
- sphelper.doIndexing(url, userName, password);
- }
- } catch (Exception) {
- throw;
- }
- }
- }
- class SPHelper {
- public void doIndexing(string SiteUrl, string UserName, string Password) {
- OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
- using(var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(SiteUrl, UserName, Password)) {
- if (clientContext != null) {
- //Give list name to select the list
- string listName = "HubflyTeam";
- //Give the filed names to be indexed
- List < string > FieldName = new List < string > () {
- "NameHF",
- "LocationHF"
- };
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(list);
- clientContext.ExecuteQuery();
- foreach(string fields in FieldName) {
- Field field = list.Fields.GetByTitle(fields);
- field.Indexed = true;
- field.Update();
- clientContext.ExecuteQuery();
- }
- Console.WriteLine("SharePoint Field Indexed Successfully!");
- Console.ReadLine();
- }
- }
- }
- }
- }
Run the project using the F5 function key. Enter the Site URL, Username and Password to access the SharePoint Site,

Step 6
Open the list setting like the below Snapshot.


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

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.

Rushi MehtaPosted Jul 28, 2018, 5:11 AM
Nice one. Thanks for sharing
Hadshana KamalanathanPosted Jul 19, 2018, 12:01 AM
Thanks for sharing
Abhishek MishraPosted Jul 18, 2018, 1:35 PM
Nice one. Loved the way it is documented.