Programmatically enforcing a field value uniqueness in SharePoint 2010

The following code snippet shows how to programmatically modify a field to enforce the uniqueness of its values (like the unique constraint in SQL Server).

SPField titleField = myList.Fields.GetField(“Title”);
titleField.Indexed = true;
titleField.EnforceUniqueValues = true;
titleField.Update();

Oh, and this only applies to SharePoint 2010, Unique fields are completely new to 2010..

Hope this helps!

Leave a comment