I have a list with 2 columns. These columns have "enforce unique values" enabled. I would like to add a new list item in CSOM. But got sometimes the duplicate error. How can I prevent this error? What is the best practice to check if the field value already exist?

// create new list item
           var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
                if (clientContext != null)
                    var list = clientContext.Web.GetListByUrl("Lists/Perioden");

                    var itemCreateInfo = new ListItemCreationInformation();
                    var listItem = list.AddItem(itemCreateInfo);
                    listItem["Title"] = titel;
                    listItem["Van"] = vanaf; // this field needs unique value
                    listItem["TM"] = tM; // this field needs unique value


1 Answer 1


You can use CamlQuery to check if there are any items with a specific field value. An example below:

SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='Van'/>
                <Value Type='Text'>{0}</Value></Eq></Where></Query></View>","vanaf");
            ListItemCollection collListItem = oList.GetItems(camlQuery);



You can check the count property of collListItem to decide how to proceed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.