0

I am trying to get all of the choices from a choice site column in SharePoint online. Using the following code I get to the success method but it errors out saying 'field' is undefined. I don't understand why this doesn't work because I have defined field in the fields() method

hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

    function fields() {
        var context = new SP.ClientContext(appweburl);
        var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);

        var appContextSite = new SP.AppContextSite(context, hostweburl);
        var web = appContextSite.get_web();
        context.load(web);

        var siteColumns = web.get_fields();
        var field = context.castTo(siteColumns.getByInternalNameOrTitle('InternalFieldName'), SP.FieldChoice);

        context.load(field);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
    }

    function onSuccessMethod(sender, args) {
        var choices = field.get_choices();
        alert("Choices: (" + choices.length + ") - " + choices.join(", "));
    }

    function onFailureMethod(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

2 Answers 2

0

You can also try REST API using following code

var appwebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var requestUrl = appwebUrl + "/_api/web/AvailableFields?$filter=InternalName eq 'InternalName'";
$.ajax({
            url: requestUrl,
            method: "GET",
            async: false,
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                var fields = data.d.results;
                var choices = fields[0].Choices.results;
}, error: function (data) { alert('Failed to load field'); } });

0
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

function fields() {
    var context = new SP.ClientContext(appweburl);
    var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);

    var appContextSite = new SP.AppContextSite(context, hostweburl);
    var web = appContextSite.get_web();
    context.load(web);

    var field = ctx.castTo(web.get_availableFields().getByTitle('fieldTitle'), SP.FieldChoice);
    //var field = context.castTo(siteColumns.getByInternalNameOrTitle('InternalFieldName'), SP.FieldChoice);

    context.load(field);
    context.executeQueryAsync(onSuccessMethod,onFailureMethod);
}

function onSuccessMethod(sender, args) {
    var choices = field.get_choices();
    alert("Choices: (" + choices.length + ") - " + choices.join(", "));
}

function onFailureMethod(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
2
  • Thanks but still getting the same error May 8, 2017 at 15:39
  • try again with updated code
    – Sunil Sahu
    May 9, 2017 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.