0

I want to update the options of the Choice field of the SharePoint List. Is there any way to update the choices using the REST API?

Below is the code i am using:

$.ajax({
        url: "https://ts.accenture.com/sites/LearningProducts/DTP/_api/web/lists/GetByTitle('DTP')/fields?$filter=(TypeAsString eq 'Choice') and (Title eq  'Status')",
        type: "POST",
        data:{
  "__metadata": {
    "type": "SP.FieldChoice"
  },
    "Choices": { 'results': ['Detractor'] }
},

        headers: {
            "accept": "application/json;odata=verbose",
             "X-RequestDigest": $('#__REQUESTDIGEST').val(),
             "Content-Type": "application/json;odata=verbose",
              "IF-MATCH": "*",


        },
        success: function (data) {
            console.log(data);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }

    });
3
  • What is the error you are getting? Did you try using 'PATCH' operation instead of 'POST' ?
    – UBK
    Apr 30, 2020 at 16:22
  • @UBK i am getting error A token was not recognized in the JSON content. i tried with Patch also but same error. Apr 30, 2020 at 19:39
  • Please use JSON.stringify(..) to convert the 'data' object or value to a JSON string. Like this: var messageData = { "__metadata": { "type": "SP.FieldChoice" }, "Choices": { 'results': ['Detractor'] } }; data: JSON.stringify(messageData)
    – UBK
    Apr 30, 2020 at 20:04

1 Answer 1

2

Sample tested script for your reference.

<script type="text/javascript">
    function updateChoiceField() {
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('mylist')/Fields(guid'2e257823-b8c6-411d-80f3-9f2cc913be0f')",
            type: "POST",
            data: JSON.stringify({ "__metadata": { "type": "SP.FieldChoice" }, "Choices": { "__metadata": { "type": "Collection(Edm.String)" }, "results": ["ChoiceA", "ChoiceB", "ChoiceC"] } }),

            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                "Content-Type": "application/json;odata=verbose",
                "X-HTTP-Method": "PATCH",
                "IF-MATCH": "*",
            },
            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                alert(JSON.stringify(error));
            }
        });
    }
</script>
<input id="Button1" onclick="updateChoiceField()" type="button" value="updateChoiceField" />

Update:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        function GetFieldOptions() {
            return $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('mylist')/Fields(guid'2e257823-b8c6-411d-80f3-9f2cc913be0f')",
                type: "GET",                
                headers: {
                    "accept": "application/json;odata=verbose",                    
                    "Content-Type": "application/json;odata=verbose"
                }
            })
        }
        function updateChoiceField() {
            GetFieldOptions().done(function (data) {
                var FieldOptions = data.d.Choices.results;
                FieldOptions.push("NewOption");
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('mylist')/Fields(guid'2e257823-b8c6-411d-80f3-9f2cc913be0f')",
                    type: "POST",
                    data: JSON.stringify({ "__metadata": { "type": "SP.FieldChoice" }, "Choices": { "__metadata": { "type": "Collection(Edm.String)" }, "results": FieldOptions } }),

                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                        "Content-Type": "application/json;odata=verbose",
                        "X-HTTP-Method": "PATCH",
                        "IF-MATCH": "*",
                    },
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (error) {
                        alert(JSON.stringify(error));
                    }
                });
            })

        }
    </script>
    <input id="Button1" onclick="updateChoiceField()" type="button" value="updateChoiceField" />
2
  • thanks it worked...but it is replacing all the field choices with the new ones specified in array. How i can append the choices with the existing ones without overwriting them? May 1, 2020 at 6:19
  • Add the logic .
    – Lee_MSFT
    May 1, 2020 at 7:09

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.