0

I am trying to create a field in sharepoint list with unique values.

  string fieldSchema = "<Field Type='" + field.FieldTypeKind + "' Indexed='" + indexing + "' DisplayName='" + field.Title + "' Name='" + field.InternalName + "' EnforceUniqueValues='" + field.EnforceUniqueValues + "' Hidden='" + field.Hidden + "' Required='" + field.Required + "'><Default>'" + field.DefaultValue + "'</Default></Field>";

                                targetcollField.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
                                targetContext.Load(targetcollField);
                                srcFieldCollections.Add(field);

But it throws exception "This field must be indexed to enforce unique values."

I have also tried

 if (field.EnforceUniqueValues == true)
                                    {
                                        field.Indexed = true;
                                        field.EnforceUniqueValues = true;
                                        field.Update();
                                        targetContext.ExecuteQuery();
                                    }

But still it throws same error.

1 Answer 1

2

The only possible reason for your error is you are trying to index the field which is not available to indexed.

Error Cases - Exceptions

There are two error cases associated with using the EnforceUniqueValues property to enforce uniqueness:

EnforceUniqueValues = true is set on a non-indexed field.

Throws an exception object (SPException) with a message reading "The field must first be indexed to enforce unique values.

EnforceUniqueValues = true is set on a list that has existing duplicate values.

Throws an exception object (SPException) with a message reading "This field contains duplicate values. Remove all duplicate values and try the operations again.

Unsupported Column Types to be indexed

  • Multiple lines of text
  • Hyperlink/Picture
  • Custom Field Types
  • Calculated Field
  • Boolean (yes/no)
  • UI version
  • Checked out to
  • Content type ID

You can get detail here.

1
  • I am doing it for single line text column
    – KumarV
    Commented Dec 12, 2016 at 9:45

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.