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.
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.
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.
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)
- {
-
- string listName = "EmployeeProfileViewsHF";
-
-
- 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.
Step 6
Open the list setting and click the columns that you used to enforce unique values like the Snapshot below.
Now, you can see the column you selected has changed Enforce unique values to Yes
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.