0

I am creating lists from javascript in my app.
Now I want to create a Field in that with unique values which is a number field.

I am using following code:

var fldCollection = oList.get_fields();

var f2 = context.castTo(
                accordionList.get_fields().addFieldAsXml("<Field Type='Number' DisplayName='AccordionOrder' Name='AccordionOrder' Required='True' Indexed='True' EnforceUniqueValues='True'/>", true, SP.AddFieldOptions.addToDefaultContentType),
                SP.FieldNumber);
f2.set_title("AccordionOrder");
f2.update();
context.executeQueryAsync(onSuccessFieldAdd, onFail);

But when the code is getting executed it gives me error like:

This field must be indexed to enforce unique values

How can I create this field unique? I have put Indexed='True' in the code. But it is not working.
Any help will be appreciated..

1

Try the following:

I am basically creating the field first and then setting the enforceUniqueValues property:

var fldCollection = oList.get_fields();

var f2 = context.castTo(
                accordionList.get_fields().addFieldAsXml("<Field Type='Number' DisplayName='AccordionOrder' Name='AccordionOrder' Required='True' />", true, SP.AddFieldOptions.addToDefaultContentType),
                SP.FieldNumber);
f2.set_title("AccordionOrder");
f2.set_required(true);
f2.set_indexed(true);
f2.set_enforceUniqueValues(true);
f2.update();
context.executeQueryAsync(onSuccessFieldAdd, onFail);
  • Thanks for the replay. But it is not working I tried. I am still able to enter two same number in list item. :( – Rahul Gokani Aug 1 '13 at 11:23
  • I tried the same code just updated f2.set_enforceUniqueValues(true); and the same error occured. – Rahul Gokani Aug 1 '13 at 11:34
  • 1
    Just had a look at the XML dfinition for field element and turn out there is no "EnforceUniqueValues" property. The XML property you are looking for is "AllowDuplicateValues = false". See here: msdn.microsoft.com/en-us/library/aa979575.aspx – Vardhaman Deshpande Aug 1 '13 at 13:14
  • Yes sir, But I found the answer from your code only. I am posting my answer. – Rahul Gokani Aug 2 '13 at 4:18
  • Can you check if the new code in my answer works? I am basically trying to make it more performance effective by making only one call to the server. Also, if my answer helped you, you can mark it as answer. This will increase your acceptance rating and people will be more inclined to help you in the future. – Vardhaman Deshpande Aug 2 '13 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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