12

I have a choice field in my list. I can get it values using server object model by following way

List<string> fieldList;

SPList spList = spWeb.Lists[listName];

SPFieldChoice field = (SPFieldChoice)spList.Fields[fieldName];

fieldList = new List<string>();

foreach (string str in field.Choices){

             fieldList.Add(str);
         }

Now I need it badly using rest api. Is it possible using rest api?

25

I have solved my problem following way

 $.ajax({
        url: "host url"+"_api/web/lists/GetByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Choice Field Name'",
        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));
        }

    });
  • What is EntityPropertyName here? – Dheeraj Oct 16 '17 at 10:49
  • 1
    That is part of the filter. Substitute your field name for 'Choice Field Name'. For example, if your field is Category, your code would read ?$filter=EntityPropertyName eq 'Category'. – CigarDoug Oct 16 '17 at 17:40
3

Below is the code for retrieving choices of choice field

function getFieldChoices(fieldName, listName) {
var dfd = $.Deferred();
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));

var urlTemplate = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('#mylist')/fields?@target='" + hostweburl + "'&$select=Choices&$filter=Title eq  '#myfield'";
var url = urlTemplate.replace("#mylist", listName).replace("#myfield", fieldName);

$.ajax({
    headers: { "Accept": "application/json;odata=verbose" },
    contentType: 'application/json',
    url: url,
    success: function (data) {
        var result = data.d.results[0].Choices.results;
        var i = result.length;
        dfd.resolve(result);
    },
    error: function (err) {
        dfd.reject(err);
    }
});
return dfd;

}

Source http://sharepointfieldnotes.blogspot.in/2013/10/sharepoint-2013-rest-code-tips.html

Hope it helps.

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.