0

I am using JSOM to create columns in my lists and not having issues for text, user, or number columns, until I attempt to add a Choice Column.

I have been trying different methods, but can't get it to create the Choice Column. Here is my current code:

        var fieldCollection = newList.get_fields();

        //add extra fields (columns) to the list & any other info needed.
        fieldCollection.addFieldAsXml('<Field Type="Number" DisplayName="My Number" Name="MyNumber" />', true, SP.AddFieldOptions.AddToDefaultContentType);
        fieldCollection.addFieldAsXml('<Field Type="Choice" DisplayName="Approval Status"><CHOICES><CHOICE>Pending</CHOICE><CHOICE>Approved</CHOICE><CHOICE>Rejected</CHOICE></CHOICES></Field>"," Name="ApprovalStatus" />', true, SP.AddFieldOptions.AddToDefaultContentType);

        context.load(fieldCollection);
  • What's with the "," Name="ApprovalStatus" />' at the end of your field xml? I would say that is your problem right there – eirikb Apr 8 '15 at 15:35
  • That is the internal name of the field, where the display name is separate. – Kode Apr 8 '15 at 15:44
  • The XML should look like this: <Field Type="Choice" Name="ApprovalStatus" DisplayName="Approval Status"><CHOICES><CHOICE>Pending</CHOICE><CHOICE>Approved</CHOICE><CHOICE>Rejected</CHOICE></CHOICES></Field> – eirikb Apr 8 '15 at 15:46
  • That did it! Thank you. Can you post your comment as an answer so I can give you credit? – Kode Apr 8 '15 at 15:52
1

Your problem is the field XML, it should look like this:

<Field Type="Choice" Name="ApprovalStatus" DisplayName="Approval Status"><CHOICES><CHOICE>Pending</CHOICE><CHOICE>Approved</CHOICE><CHOICE>Reject‌​ed</CHOICE></CHOICES></Field>
1

Try sample below

<script type="text/ecmascript">
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    //Geting reference to the list
    oList = clientContext.get_web().get_lists().getByTitle('CustomList');

    // Get filed collection
    var fldCollection = oList.get_fields();

    var f2 = clientContext.castTo(
                oList.get_fields().addFieldAsXml('<Field Type="Choice" DisplayName="choice" Name="fldchoice" />', true, SP.AddFieldOptions.addToDefaultContentType),
                SP.FieldChoice);
    var choices = Array("Approved", "Rejected", "Progress");
    f2.set_choices(choices);
    f2.update();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );

    function onQuerySucceeded() {
        alert("List Field Updated");
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

Source - http://melick-rajee.blogspot.com/2013/03/add-fields-to-sharepoint-2013-list.html

  • I saw that method, but it didn't fit within the method I am following. I can adjust but was hoping to keep it inline with fieldCollection.addFieldAsXml('<Field Type="Choice" DisplayName="Approval Status"><CHOICES><CHOICE>Pending</CHOICE><CHOICE>Approved</CHOICE><CHOICE>Rejected</CHOICE></CHOICES></Field>"," Name="ApprovalStatus" />', true, SP.AddFieldOptions.AddToDefaultContentType); – Kode Apr 8 '15 at 14:56

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.