2

I am trying to get the choice field value in select tag from SharePoint list.

Here, the code is given below:

function GetphysicalVertual() {
    var defPG = $.Deferred();
    var inputElement;

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('List Name')/fields?$filter=EntityPropertyName eq 'Column Name'",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            inputElement = '<option value="0">--Select--</option>';
            for (var i = 0; i < data.d.results; i++) {
                var locationName = data.d.results[i].Choices.results;
                inputElement += '<option value="' + locationName + '">' + locationName + '</option>';
            }
          
            console.log(inputElement);
            $('#ddlphysicalVirtual').append(inputElement);
            $('#ddlphysicalVirtual').each(function () {
                $('option', this).each(function () {
                    if ($(this).val() == '0') {
                        $(this).attr('selected', 'selected');
                    }
                });
            });
            defPG.resolve(data);
        },
        error: function (data) {
            defPG.reject(data);
        }
    });
    return defPG.promise();
}

Using this code I am not able to get the value.

2
  • Are you getting any error using this API call? Are you trying to get the choices options from choice field OR the value of selected choice option against list item(just confirming)? Jul 26 at 5:43
  • No, i am not getting any error but i am also not getting the choices options from choice field.
    – Shreya
    Jul 26 at 6:44
2

Try using below code which is working fine for me:

<script type="text/javascript" src="/sites/SPConnect/SiteAssets/jquery.min.js"></script>

<select name="cars" id="ddlphysicalVirtual"></select>
<button type="button" onclick="getChoiceOptions()"> Get Choice Options </button>

<script type="text/javascript">
    function getChoiceOptions() {
        var defPG = $.Deferred();
        var inputElement = "";
        
        var fieldsEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Color'";

        $.ajax({
            url: fieldsEndpoint,
            type: "GET",
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {              
                inputElement += '<option value="0"> --Select-- </option>';
    
                var choicesArray = data.d.results[0].Choices.results
                for (var i = 0; i < choicesArray.length; i++) {
                    var locationName = choicesArray[i];
                    inputElement += '<option value="' + locationName + '">' + locationName + '</option>';
                }
              
                $('#ddlphysicalVirtual').append(inputElement);
    
                $('#ddlphysicalVirtual').each(function () {
                    $('option', this).each(function () {
                        if ($(this).val() == '0') {
                            $(this).attr('selected', 'selected');
                        }
                    });
                });
                
                defPG.resolve(true);
            },
            error: function (error) {
                console.log(error);
                defPG.reject(false);
            }
        });
        return defPG.promise();
    }
</script>

Output:

enter image description here

1
  • 1
    thanks, it works for me
    – Shreya
    Jul 26 at 9:15

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.