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!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s