1

I am trying to retrieve managed metadata column (NewsCategory) value in SharePoint 2013 Document library using JSOM. I get "Object Object" rather than actual value.

I tried:-

var newsCat = item.get_item('NewsCategory');

alert(newsCat); //Displays [Object Object]

var newsCatLabel = newsCat.get_label();

var newsCatId = newsCat.get_termGuid();

But, I get the error "Object doesn't support property or method get_label()"

I also tried :-

   var newsTags = item.get_item(' NewsCategory ');

   for (var i = 0; i < newsTags.get_count() ; i++) {

   var newsTag = newsTags.getItemAtIndex(i);

   var newsTagLabel = newsTag.get_label();

   var newsTagId = newsTag.get_termGuid();

   }

Even now I get the error "Object doesn't support property or method get_count()"

I have included " NewsCategory " in the load request:-

context.load(items, 'Include(File, NewsCategory)');

Any idea what the issue is? Do I have to add any *.js file using $.getScript?

I added following .js files

 var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";

$.getScript(scriptbase + "SP.Runtime.js", function () {

    $.getScript(scriptbase + "SP.js", function () {

        $.getScript(scriptbase + "SP.Core.js", function () {


        });
    });
});

Thanks in advance

3

It depends whether managed metadata column is a single-valued (SP.Taxonomy.TaxonomyFieldValue) or multi-valued (SP.Taxonomy.TaxonomyFieldValueCollection)

It seems in your case the type of managed metadata column is SP.Taxonomy.TaxonomyFieldValueCollection.

The following example demonstrates how to determine managed metadata field type:

var newsTags = listItem.get_item('NewsCategory');
var fieldTypeName = newsTags.constructor.getName();

From another hand, I guess there is a typo in your example at line:

var newsTags = item.get_item(' NewsCategory '); //why extra space in field name?

Try to replace it with this one:

var newsTags = listItem.get_item('NewsCategory');
for (var i = 0; i < newsTags.get_count() ; i++) {
    var newsTag = newsTags.getItemAtIndex(i);
    var newsTagLabel = newsTag.get_label();
    var newsTagId = newsTag.get_termGuid();
}
  • Actually space was induced when editing the Script. I face the same issue without space. – user117048 May 20 '14 at 20:32
  • Could you please place the line: console.log(newsTags.constructor.getName()); after the line: var newsTags = item.get_item(' NewsCategory '); to print the type of the managed metadata field – Vadim Gremyachev May 20 '14 at 21:02
  • The type "newsTags.Constructor.getName()" prints the type as string. – user117048 May 21 '14 at 23:40
  • Thanks "Vadim" for your reply. The issue got resolved. I was using the wrong "listItem" object. – user117048 May 21 '14 at 23:51
  • any advice how to do it by C#? – user31624 Jul 16 '14 at 20:47
1

Step 1: Get the Term Set ID-->

  • Go to Central Administration > Manage service applications under Application Management > Managed Metadata Service
  • Get the following field details: Central Admin URL, Term Store Name, Group Name, Term Set Name enter image description here
  • Open the SharePoint Powershell using Run as Administrator
  • Execute the following Script:
$taxonomySession = Get-SPTaxonomySession -Site ""
$termStore = $taxonomySession.TermStores[""]
$group = $termStore.Groups[""]
$termSet = $group.TermSets[""]
$termSet
  • Get the Id. This is the TermSetID enter image description here Step 2: Fetch the Terms using JavaScript-->
  • Go to your Site and in the Site Assets upload jquery-1.11.1.min.js and Script.js. Script.js will contain following code:

var context;
//Term Store Name
var TermStoreName = 'Managed Metadata Service';
//Term Set ID
var PublicationId = '6dc72cb0-9ea2-4dbd-8559-1a3b555b2ca8';
//Array of Terms
var PublicationTerms = [];

function TermObject(termId,termName){
    var self = this;
    self.TermId = termId;
    self.TermName = termName;
}

$(document).ready(function () {
    context = SP.ClientContext.get_current();
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    var termStores = taxSession.get_termStores();
    var termStore = termStores.getByName(TermStoreName);
    var currentTermSet = termStore.getTermSet(PublicationId);
    context.load(currentTermSet);
    context.executeQueryAsync(
    function () {
        var terms = currentTermSet.get_terms();
        context.load(terms);
        context.executeQueryAsync(
        function () {
            var termsEnum = terms.getEnumerator();
            while (termsEnum.moveNext()) {
                var currentTerm = termsEnum.get_current();
                //Get the Term ID
                var termid = currentTerm.get_id();
                //Get the Term Name
                var termname = currentTerm.get_name();
                alert(termname);
                PublicationTerms.push(new TermObject(termid,termname));
            }
        },
        function (sender, args) {
            console.log(args.get_message());
        });
    },
    function (sender, args) {
        console.log(args.get_message());
    });
});

  • Come to the page where you need to fetch the terms. Refer the following files
/_layouts/15/SP.Runtime.js" type="text/javascript">
/_layouts/15/SP.js" type="text/javascript">
/_layouts/15/SP.Taxonomy.js" type="text/javascript">
/SiteAssets/jquerygoo-1.11.1.min.js" type="text/javascript">
/SiteAssets/Script.js" type="text/javascript">
  • Save the Page and check the result: enter image description here enter image description here

Best of Luck!!

  • Ranjan's Method worked for me.. but it gives me all Terms stored in my taxonomy term store group... I am customizing a document set docsethomepage.aspx and I need to get the metadata term of the displayed document set... How can I archieve this?? – Félipé Apr 20 '18 at 9:24

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.