0

I have a managed metadata type column in my document library. I need to get the data from this column by using jsom. But whenever I try to get the values it gives me error. The code and error are given below:

var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle(docLibName);


var camlQuery = new SP.CamlQuery();
var query = '<View><Query><Where><Eq><FieldRef Name=\'ServiceOffering\' /><Value Type=\'TaxonomyFieldTypeMulti\'>' + serviceOffering + '</Value></Eq></Where></Query><ViewFields><FieldRef Name=\'Title\'/></ViewFields></View>';
//var query = '<View><Query><Where><Eq><FieldRef Name=\'ServiceOffering\' /><Value Type=\'TaxonomyFieldTypeMulti\'>' + serviceOffering + '</Value></Eq></Where></Query></View>';

//alert("query : " + query);
camlQuery.set_viewXml(query);

this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem,'Include(DisplayName,FileRef,DocumentType)');   

var listItemEnumerator = collListItem.getEnumerator();

var count = collListItem.get_count();       

alert("count: " +count);

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
   alert("Value: " + oListItem.get_item('DocumentType').get_label());
}

oListItem.get_item('DocumentType').get_label() is the place that throws error and the error is

"The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. when getting data from managed metadata field"

Also note that the column has been created using visual studio.

1

You can use this for Single select Managed metadata fields and Multislelect Managed metadata fields:

  while (listItemEnumerator.moveNext()) {
   var oListItem = listItemEnumerator.get_current();      
   ID_=oListItem.get_item('ID');     
   Name_=oListItem.get_item('Name');  
   MMD1_=oListItem.get_item('Nation').get_label();//single value MM field
  alert(MMD1_);

  MMD2=oListItem.get_item('Status').getEnumerator();//multivalue value MM field
  while (MMD2.moveNext()) {
        var oListItem2 = MMD2.get_current();      
        var label = oListItem2.get_label();
        alert(label);
    }
3

First of all, in your example the SP.ClientContext.executeAsync method that submits request to server is missing.

Secondly, it depends on the type of Managed Metadata field, see the below example how to retrieve managed metadata field value(s):

And thirdly, since you are explicitly specifying what fields to retrieve using SP.ClientContext.load method, there is no need to specify fields using ViewFields element in CAML query.

So, remove ViewFields element from CAML query, for example:

var qry = new SP.CamlQuery();
qry.set_viewXml('<View><Query>your query goes here</Query></View>');

CAML ViewFields vs SP.ClientContext.load method

The key difference between CAML ViewFields vs SP.ClientContext.load` method is that in the first case the system fields are also included in the result.

Example

Assume a Documents library contains:

  • single-valued managed metadata field named DocumentType
  • multi-valued managed metadata field named DocumentTypes

then the following example demonstrates how to retrieve managed metadata field values:

var listTitle = "Documents";

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items,'Include(DisplayName,FileRef,DocumentType,DocumentTypes)');
ctx.executeQueryAsync(
    function () {
      var e = items.getEnumerator();
      while (e.moveNext()) {
          var item = e.get_current();
          //get single-valued managed metadata field value
          var docTypeVal = item.get_item('DocumentType');  
          if(docTypeVal)
             console.log(docTypeVal.get_label());

          //get multi-valued managed metadata field value
          var docTypeVals = item.get_item('DocumentTypes');  
          if(docTypeVals){
              for(var i = 0; i < docTypeVals.get_count();i++){
                  var val = docTypeVals.getItemAtIndex(i);
                  console.log(val.get_label());
              }
          }

      }          
    }, 
    function(sender,args){
        console.log(args.get_message());
    });   

Some recommendations:

1) Do not use new operator with SP.ClientContext.get_current() method, the following example demonstrates how to get current SP.ClientContext:

var ctx = SP.ClientContext.get_current();

2) Verify whether managed metadata field value is null, for example:

var docTypeVal = item.get_item('DocumentType');
if(docTypeVal)
   console.log(docTypeVal.get_label()); 
  • When I include the managed metadata field in my Include statement, it gives me an error which says "Unable to get property 'TaxonomyFieldValue' of undefined or null reference". – Mancy Desaee Feb 6 '15 at 12:43
  • The answer has been updated (see the third item). Basically this error occurs since the requested fields are missing in ViewFields, please remove ViewFields element from CAML query – Vadim Gremyachev Feb 6 '15 at 13:27
  • I have removed the ViewFields from the query and the entire code it similar to what you have stated in the example but yet it does not work and gives the error when it tries to access the Managed metadata field. The error message is mentioned in my first comment above. – Mancy Desaee Feb 6 '15 at 13:38
  • 1
    @VadimGremyachev, +1, I always make it a point to read your answers. Every time there is a new learning. The key difference between CAML ViewFields vs SP.ClientContext.load method is that in the first case the system fields are also included in the result. – Nadeem Yousuf Feb 6 '15 at 13:53
2

Are you including 'DocumentType' when loading the list item collection? Like:

context.load(collListItem, 'Include(DocumentType)')

UPDATE

Try either updating <ViewFields> in theCAML Query to include fields liek DisplayName,FileRef,DocumentType or remove <ViewFields><FieldRef Name=\'Title\'/></ViewFields> from query. Also, if you define them in the query, then don't load them during context.load

  • Thank you @Nadeem Yousuf. No, when I do that I am not able to get the count. – Mancy Desaee Feb 6 '15 at 11:06
  • @MancyDesaee, can you share that part of the code as well? – Nadeem Yousuf Feb 6 '15 at 11:08
  • @MancyDesaee, see my updated answer. – Nadeem Yousuf Feb 6 '15 at 11:25
  • Whenever I add all the fields in ViewFields it gives another error and if it is removed it wont process the query and return any of the items. – Mancy Desaee Feb 6 '15 at 11:43
  • Just noticed that you are not executing the query. clientContext.executeQueryAsync is missing – Nadeem Yousuf Feb 6 '15 at 11:48
1

Using clientContext.load() alone doesn't actually load your data, it marks it as to be loaded for the next time data is fetched from the server. This is a common misconception when using JSOM and you'd probably find numerous similar questions if you search your error on this site.

The solution is rather simple, you have to actually request the data:

clientContext.load(collListItem,'Include(DisplayName,FileRef,DocumentType)');
clientContext.executeQueryAsync(function () {
    //Request succeeded
    var listItemEnumerator = collListItem.getEnumerator();
    //Work with your data here
}, function () {
    //Request failed
});
  • I have done this very thing but it is not working. Whenever I access the data from the DocumentType column, it gives me error. – Mancy Desaee Feb 6 '15 at 12:04
  • DocumentType is managed metadata field as mentioned. – Mancy Desaee Feb 6 '15 at 12:06
0

Finally got an answer to this. The issue was in the site column which had been created using Visual Studio. The Source ID attribute was missing and the mapping of the hidden column field with the actual column was missing.

<Field Type="TaxonomyFieldTypeMulti"  Mult="TRUE" Sortable="FALSE"  DisplayName="Practice" List="Lists/TaxonomyHiddenList" WebId="~sitecollection" ShowField="Term1033" Required="TRUE" EnforceUniqueValues="FALSE" Group="Shop Window Group" ID="{76A62E52-23C2-415C-9FA8-FE8D8D7D9276}"  StaticName="Practice" Name="Practice" Overwrite="TRUE"  xmlns="http://schemas.microsoft.com/sharepoint/">
<Default />
<Customization>
  <ArrayOfProperty>
    <Property>
      <Name>SspId</Name>
      <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">3ed02d6a-48b9-483f-8d8e-db54a3f09e00</Value>
    </Property>
    <Property>
      <Name>GroupId</Name>
    </Property>
    <Property>
      <Name>TermSetId</Name>
      <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">a513af8e-0531-42f6-9aef-2c6ae92637f7</Value>
    </Property>
    <Property>
      <Name>AnchorId</Name>
      <Value xmlns:q3="http://www.w3.org/2001/XMLSchema" p4:type="q3:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">00000000-0000-0000-0000-000000000000</Value>
    </Property>
    <Property>
      <Name>UserCreated</Name>
      <Value xmlns:q4="http://www.w3.org/2001/XMLSchema" p4:type="q4:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
    </Property>
    <Property>
      <Name>Open</Name>
      <Value xmlns:q5="http://www.w3.org/2001/XMLSchema" p4:type="q5:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
    </Property>
    <Property>
      <Name>TextField</Name>
      <Value xmlns:q6="http://www.w3.org/2001/XMLSchema" p4:type="q6:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{F740FED8-2CC1-4E58-BEA2-9807AAB1B10B}</Value>
    </Property>
    <Property>
      <Name>IsPathRendered</Name>
      <Value xmlns:q7="http://www.w3.org/2001/XMLSchema" p4:type="q7:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
    </Property>
    <Property>
      <Name>IsKeyword</Name>
      <Value xmlns:q8="http://www.w3.org/2001/XMLSchema" p4:type="q8:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
    </Property>
    <Property>
      <Name>TargetTemplate</Name>
    </Property>
    <Property>
      <Name>CreateValuesInEditForm</Name>
      <Value xmlns:q9="http://www.w3.org/2001/XMLSchema" p4:type="q9:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
    </Property>
    <Property>
      <Name>FilterAssemblyStrongName</Name>
      <Value xmlns:q10="http://www.w3.org/2001/XMLSchema" p4:type="q10:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Value>
    </Property>
    <Property>
      <Name>FilterClassName</Name>
      <Value xmlns:q11="http://www.w3.org/2001/XMLSchema" p4:type="q11:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy.TaxonomyField</Value>
    </Property>
    <Property>
      <Name>FilterMethodName</Name>
      <Value xmlns:q12="http://www.w3.org/2001/XMLSchema" p4:type="q12:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">GetFilteringHtml</Value>
    </Property>
    <Property>
      <Name>FilterJavascriptProperty</Name>
      <Value xmlns:q13="http://www.w3.org/2001/XMLSchema" p4:type="q13:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">FilteringJavascript</Value>
    </Property>
  </ArrayOfProperty>
</Customization>

The ID of the HTField and the ID in TextField have to match.

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.