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 a 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.

How To Enforce Unique Value For SharePoint Columns Using C#

Step 2

In Templates, select Visual C#, select Console App (.NET Framework) and give an appropriate name in name textbox and then click the OK button.

 

How To Enforce Unique Value For SharePoint Columns Using C#

 

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.

How To Enforce Unique Value For SharePoint Columns Using C#

Step 4

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

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

Step 5

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

How To Enforce Unique Value For SharePoint Columns Using C#

 

Step 6

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

How To Enforce Unique Value For SharePoint Columns Using C#


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

 

How To Enforce Unique Value For SharePoint Columns Using C#

 

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