13

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?

26

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));
        }

    });
| improve this answer | |
  • What is EntityPropertyName here? – Dheeraj Oct 16 '17 at 10:49
  • 3
    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.

| improve this answer | |
0

This is how I did it in React. A slight change in the call structure, but easily altered to the ajax call.

I have a SharePoint Picture Library (but a normal list works the same way). One of the columns is a Choice list of locations, e.g. "Perth", "Melbourne", etc. By using the following query I get an object back of all fields in the Picture library. e.g.

 let response: SPHttpClientResponse = await this.props.Context.spHttpClient
 .get(
  this.props.Domain + 
  this.props.Path + 
  this.props.ParentSiteName + 
  `/_api/web/lists/getbytitle('` + 
  this.props.SPListNames.Floorplans + 
  `')/fields?$filter=InternalName eq 'Location'`,
  SPHttpClient.configurations.v1
);

const Response = await response.json();

console.log('Response');
console.log(Response);

The key part of the query was: this.props.Domain + this.props.Path + this.props.ParentSiteName + /_api/web/lists/getbytitle(' + this.props.SPListNames.Floorplans + ')/fields?$filter=InternalName eq 'Location'

Which is: https://sharepointtenantname.sharepoint.com/sites/name/_api/web/lists/getbytitle('listname')/fields?$filter=InternalName eq 'Location'

Where InternalName is the attribute of the response object I get back from the SharePoint call that I want to filter on using my text 'Location'. If you are unsure of what to filter on, do a test call to this api to see the object that is returned and built the call based on your needs from that: https://sharepointtenantname.sharepoint.com/sites/name/_api/web/lists/getbytitle('listname')/fields

In the result, the object has a Choices attribute, which is an array of strings with all the choices I am interested in. I can use this to complete my task.

| improve this answer | |

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.