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 Answer 1

    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); 
    7
    • Thanks for the replay. But it is not working I tried. I am still able to enter two same number in list item. :(CommentedAug 1, 2013 at 11:23
    • I tried the same code just updated f2.set_enforceUniqueValues(true); and the same error occured.CommentedAug 1, 2013 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.aspxCommentedAug 1, 2013 at 13:14
    • Yes sir, But I found the answer from your code only. I am posting my answer.CommentedAug 2, 2013 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.CommentedAug 2, 2013 at 5:05

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.