Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

How To Add Index On SharePoint Fields Using C#

In this article, I am going to give the steps required 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?

1. Indexed columns allow for fast retrieval.
2. It improves the performance of the database.
3. It reduces the disc access while performing a query.

Step 1:

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

Visual Studio Project creation

Step 2:

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

Visual Studio Project selection

Step 3:

I. To install SharePoint PnP Core Library in the c# project to perform SharePoint field indexing operations on SharePoint site, refer this blog: How To Install SharePoint PnP Core Library In Visual Studio 2017
II. After installing SharePoint core library . You will see the SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll in your project reference.

SP field Indexing

Step 4:

I. 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();

                }

            }

        }

    }

}

Step 5:

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

SPFILED indexing

Step 6:

I. Open the list setting like the below shown Snapshot

Settings

II. Drag down and click Indexed columns like below snapshot

Indexed columns

III. That's it - SharePoint Field is now indexed successfully.

SharePoint Field

These are the steps to create SharePoint field index programmatically using c# code. Feel free to fill up the comment box below if you need any assistance.