0

Is it possible to dynamically add new choice field values/options while creating a new list item through rest API if I check 'Allow fill in choice' to true?

Example

The choice field 'ChoiceFieldName' has 2 options at beginning. Say, Option1 and Option2.

I am doing a Rest API POST request to create a new list item and mentioned new value (Option3) for the 'ChoiceFieldName'. So, after the POST request submitted now the 'ChoiceFieldName' has 3 options. ie, Option1 , Option2 and Option3

Is it possible?

1

1 Answer 1

1

Fill-in values will not be added to choice column options automatically.

You have to update the choice options using /fields endpoint in SharePoint REST API.

Here's sample example for this:

<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" />

Reference: Update/Add choices of a Choice field of SharePoint list using REST API

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.