1

I have some list "SiteInformation". In this list there is always 1 list item. In this list item there is a managed metadata column.

I would like to create some script editor webpart. Inside this webpart I would like to show the managed metadata values (multiple). How to do this?

For now I have this logic to get the title. The next step is to get managed metadata values from 1 column "Countries":

getSiteTitle = function(requestUrl, success, fail){
        $.ajax({
            url: requestUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                success(data);

            },
            error: function (data) {
                fail(data);
            }
        });
}
$(document).ready(function() {
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('SiteInformation')?$top=1&$select=Title&$orderby=ID";
    getSiteTitle(url, function(data) {
        var title = "";
        var results = data.d.results;
        if(results != null && results.length > 0) {
            title = results[0].Title;
        }
        //logic to update title in your container
    }, function(error) {
        alert("Failed to load title");
    }
});
1

Since your managed metadata column is going to be multi-valued, your results, will be in the form of any array.

Assuming that, the internal column name is Countries, you can use the below code:

getSiteTitle = function(requestUrl, success, fail){
        $.ajax({
            url: requestUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                success(data);

            },
            error: function (data) {
                fail(data);
            }
        });
}
$(document).ready(function() {
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('SiteInformation')?$top=1&$select=Title,Countries&$orderby=ID";
    getSiteTitle(url, function(data) {
        var title = "";
        var countries = "";
        var results = data.d.results;
        var countriesArray;
        if(results != null && results.length > 0) {
            title = results[0].Title;
            //it will be an array
            countriesArray = results[0].Countries.results;
            countriesArray.forEach(function(country) {
                //semi-colon separated country names
                countries+= country.Label + ';';
            });

        }
        //logic to update title in your container
    }, function(error) {
        alert("Failed to load title");
    }
});
  • hi @ola, did you try this ? is it working for you ? – Gautam Sheth Jun 28 '17 at 16:17

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.