Tuesday, 2 June 2015

Check if whether a field is exist in list by REST Call


We can check whether a field is existed in a sharepoint list by means of REST call. The following code illustrates the scenario:



    var hostweburl;
    var appweburl;

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

function isFieldExist(listName, fieldName, callback) {

    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/getbytitle('" + listName + "')/fields?$Filter=StaticName eq '" + fieldName + "'&@target='" + hostweburl + "'",
        method: "GET",
        headers: {
            "content-type": "application/json; odata=verbose", "Accept": "application/json;odata=verbose"
        },
        success: function (data) {
            var fields = JSON.parse(data.body).d.results;
            if (fields.length == 0) callback({ Status: false, Field: null });

            else callback({ Status: true, Field: fields[0] });
        },
        error: function (error) {
            callback({ Status: false, Field: null });
        }
    });


}


No comments:

Post a Comment