Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

How To Enforce Unique Value For SharePoint Columns Using C#

In this article, we are going to see the steps required to enforce unique value for SharePoint columns using C#.  You can enforce uniqueness on values for SharePoint list or library columns. If you enforce Person or Group column, the list item in the target list cannot have the same Person or Group on a child list.  

You can enforce unique columns only to the below supported columns types. They are: 

→ Person or Group (but not multi value) 
→ Title (but not in a document library) 
→ Choice field (but not multi choice) 
→ Number 
→ Lookup (but not multi value) 
→ Currency 
→ Date/ Time 

And the unsupported column types are: 

→ Multiple lines of text 
→ Custom Field Types 
→ Boolean (yes/no) 
→ Modified time 
→ UI version 
→  Created time 
→ Checked out to 
→ Calculated Field 
→ Modified by 
→ Content type ID 
→ Hyperlink/Picture 

Step 1: 

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

Open Visual Studio

Step 2: 

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

Templates Selection

Step 3: 

→ Steps to take are: Install SharePoint PnP Core Library in the C# project for indexing SharePoint field operations at the SharePoint site. The following blog can be referenced for installing the library.  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. 

SharePoint core library

Step 4: 

→ Open the program.cs file and type the below code  

using System; 

using System.Collections.Generic; 

using Microsoft.SharePoint.Client; 

namespace Enforce_Coumns 

{ 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            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.doEnforceUnique(url, userName, password); 

            } 

        } 

    } 

    class SPHelper 

    { 

        public void doEnforceUnique(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 = "EmployeeProfileViewsHF"; 

                    //Give the filed names to be indexed 

                    List<string> FieldName = new List<string>() { "UserHF" }; 

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

                    clientContext.Load(list); 

                    clientContext.ExecuteQuery(); 

                    foreach (string fields in FieldName) 

                    { 

                        Field field = list.Fields.GetByTitle(fields); 

                        field.EnforceUniqueValues = true; 

                        field.Update(); 

                        clientContext.ExecuteQuery(); 

                    } 

                    Console.WriteLine("SharePoint Field Enforced Uniqueness Successfully!"); 

                    Console.ReadLine(); 

                } 

            } 

        } 

      } 

    } 

} 

Step 5: 

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

Run the project

Step 6: 

→ Open the list setting and click the columns that you used to enforce unique values like the Snapshot below.   

Open the list setting

Now you can see the column you selected as changed to Enforce unique values to Yes  

Sharepoint Site Column

These are the steps to enforce unique value for SharePoint columns programmatically using c# code. Feel free to fill up the comment box below, if you need any further assistance.