4

I'm using JS to create application based on SharePoint.

What I have: field in list item, with dropdown and few values that are not list (just few static values). I suppose this dropdown created using some special magic of SP UI.

What I can't figure out: how to retrieve this options for droplist using REST or JSOM to create my own dropdown but with same values.

I can hardcode it right now, but you know why it's not a solution.

Thanks!

6

In SharePoint, I assumed that Column type is "Choice field".

Below is the code for get all choices from that choice field:

$.ajax({
        url: "Site 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));
        }
    });

Reference Link : https://social.technet.microsoft.com/Forums/ie/en-US/18defe52-d211-4720-993b-5e6197457e20/how-to-get-choice-fields-values-using-rest-api-or-jsom?forum=sharepointdevelopment

Hope it works for you.

3

Option 1

Using REST API, you can try below code which relies on the display name of the choice column:

$.ajax({
        url: "https://sitecollectionurl/_api/web/lists/getByTitle('ListName')/fields/getbytitle('ChoiceField')",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            console.log(data.d.results[0].ChoiceField.results);
        },
        error: function (error) {
            console.log(JSON.stringify(error));
        }
    });

Option 2 - using JSOM

var distinctChoices = [];
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('CustomList');
var field = list.get_fields().getByInternalNameOrTitle("ChoiceFieldName");
var choiceField = ctx.castTo(field, SP.FieldChoice);

ctx.load(field);

ctx.executeQueryAsync(function () { 
    distinctChoices = choiceField.get_choices();        
},
function (sender, args) {
    console.log(args);
});

Reference - Get choice field using JSOM

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.