In this article we will be seeing how to enforce unique value for a list or library column in SharePoint 2010 using C# and powershell. In SharePoint 2010 we can enforce uniqueness on values in a list or library column, effectively creating a primary key. When you create a column in the list or library you could see an option "Enforce Unique Values" where you can enforce uniqueness. When you enforce uniqueness on a Lookup column, the list item in the target list can have only one list item looking up to it from the child list. Using C#:
namespace EnforceUniqueValue { class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://serverName:1111/")) { using (SPWeb web = site.RootWeb) { SPList list = web.Lists.TryGetList("cl"); SPField field = list.Fields["TestCol"]; field.Indexed = true; field.EnforceUniqueValues = true; field.Update(); } } } } }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: