Tuesday, 2 June 2015

Change look up field to allow multiple values by means of REST Call



It is possible change a look up field of a SharePoint list to allow multiple values by means of REST Call. The following code snippets illustrates the scenario:





    var hostweburl;
    var appweburl;

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

function changeLookupFieldToAllowMultipleValues(listName, fieldName, callback) {

    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/getbytitle('" + listName + "')/fields/getbytitle('" + fieldName + "')?@target='" + hostweburl + "'",
        method: "POST",
        body: "{ '__metadata': { 'type': 'SP.FieldLookup' }, 'AllowMultipleValues': true }",
        headers: {
            "content-type": "application/json; odata=verbose", "Accept": "application/json;odata=verbose", "X-HTTP-Method": "MERGE"
        },
        success: function (data) { callback({ Status: "Success", Message: data }); },
        error: function (error) { callback({ Status: "Error", Message: error }); }
    });

}


Test it on your own environment.


Happy SharePointing.....................:)

No comments:

Post a Comment