Monday 25 September 2017

Get HTML dropdown field values from SharePoint choice list column.

// Call methods on page load
$(function () {

    var ddl1 = bindDropdownValues("ColumnName1", "#ColumnName1_Id");
    var ddl2 = bindDropdownValues("ColumnName2", "#ColumnName2_Id");
    var ddl3 = bindDropdownValues("ColumnName3", "#ColumnName3_Id");

});

// This will make a rest call and on success of that, it will bind the list choice fields values to form dropdown controls.
function bindDropdownValues(columnName, ControlID) {
    var dfd = new $.Deferred();
    getChoiceValues("" + columnName + "").done(function (platformValues) {

        $.each(platformValues.d.results[0].Choices.results, function (key, values) {

            $("" + ControlID + "").append($("<option class='ddlValues'></option>").text(values));
        });
        dfd.resolve();
    });
    return dfd.promise();
}

// This is the rest call to get the list choice field data.
function getChoiceValues(columnName) {
    return $.ajax
    ({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LIST NAME HERE')/fields?$filter=EntityPropertyName eq '" + columnName + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
    });

}


--------------------------------------------END----------------------------------------------

No comments:

Post a Comment