3

I have a choice field Status with choice values Approved,Rejected and Hold. How do I retrieve these choices using REST Api?

PFB the code:

$.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/GetByTitle('Questions')/fields?$filter=EntityPropertyName eq 'Status'",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            console.log(data.d.results[0].Choices.results);

        },
        error: function (error) {
            alert(JSON.stringify(error));
        }

    });        

Thanks in advance

2

To get the choice values from listdata.svc you need to make a request against the correct EntitySet...

You can think of an EntitySet as any list that listdata.svc has access to -- this includes choice field choices.

Choice field EntitySets will typically have an endpoint name that looks like:

<ListName><ChoiceFieldName>

So if I have a list called MyList which has a choice field called MyChoiceField, then a call to the entity set name that can be accessed by listdata.svc would be have an endpoint of:

_vti_bin/listdata.svc/MyListMyChoiceField

To find the precise name of the EntitySet you should be looking for, make a call to the top level endpoint listdata.svc in the site you're interested in.

Use something like:

$.getJSON('../_vti_bin/listdata.svc').then(function(data) { console.log(data) });

In your browser dev tools console, expand the result object. The results should be alphabetical by default, find your list name then below it you should see some more choices for choice fields within that specific list.

When you locate the correct endpoint name, just make a call to that EntitySet name like:

$.getJSON('../_vti_bin/listdata.svc/MyListMyChoiceField')
    .then(function(data) { console.log(data) });

The results to the call against the choice field endpoint will give you a results object which is an Array of objects representing the available choices for that field. You can access the values of the choices by using data.d.results[i].Value in the callback.

  • Thanks for your answer and your time but am very new to rest api so i am not able to understand the whole thing that you have explained....Is it possible for you to re-edit my code with the right answer? – Dheeraj Oct 16 '17 at 11:05
  • Better please refer this links: Link1, Link2, Link3 – san Oct 16 '17 at 11:10
1

Refer below code. This will work for you.

(Note: Here choiceCol is the name of my choice field. You will have to change it accordingly).

var url = "/_api/web/lists/getbytitle('Questions')/items?$select=choiceCol";

getItems(url).then(getItemsSuccess, getItemsFail);

function getItems(url){
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        }
    });
}

function getItemsSuccess(data){
    var result = data.d.results; // set value of result variable 
    $.each(result, function(key, value){
        console.log(value.choiceCol);   // access the choice field value
    });
}

function getItemsFail(err){
    // error callback
    debugger;
}

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.