Friday, 22 February 2013

SharePoint 2013 Taxonomy field CSOM and jQuery

Wow what a day was yesterday! Felt like I was in an exam and running against time. Yes it was a lot of pressure to create the user News subscription model displayed in a Div. 

I have a simple list with two single line text columns username and the news channel. This list is populated by a webpart.

News Channel Subscription
Internal 1
Internal 1

Using second column from this custom list I had to do a lookup on pages library.

My immediate guess was to use jQuery with REST oData and will help me rapidly develop my script. But what do you know! when I started constructing the link 

It gave me the following error

The field 'News_x002d_Information_x002d_Type' of type 'TaxonomyFieldType' cannot be used in the query filter expression.

My next selection was to use CSOM (JavaScript) and CAML query. But I soon realised that since the column I wish to filter on, is Taxonomy column therefore I cannot use text values. Instead I had to do a lookup first in a hidden list called 'TaxonomyHiddenList' at the root site and fetch back the WSSID . 

Then using the WSSID I constructed the CAML query for Pages library. This can be seen below:

                                                     '<In><FieldRef Name=\'News_x002d_Information_x002d_Type\'  LookupId=\'TRUE\'  />'  + 
                                                      ' <Values>' + 
                                                                 <Value Type=\'Lookup\'>102</Value>'+
                                                                 <Value Type=\'Lookup\'>104</Value>'+
                                                      '</Values>'  +  
                                                     '</In></Where>' + 
var     oList    = hubSiteCtx.get_web().get_lists().getByTitle('Pages');   
collListItem   =  oList.getItems(camlQuery);
hubSiteCtx.executeQueryAsync(  Function.createDelegate(this,
this.onGetNewsPagesSucess)  , Function.createDelegate(this, this.onGetNewsPagesFailed) ); 

in above CAML query I constructed the string

                                                                 <Value Type=\'Lookup\'>102</Value>'+
                                                                 <Value Type=\'Lookup\'>104</Value>'+

after the lookup from custom list and Hidden taxonomy list

Second challenge :

The above query returned the data. All simple fields like title, name etc came back fine but the Taxonomy field was returning value = [object] when I used following statement


I tried other methods like


I could clearly see that above method was returning back lots of values for the taxonomy column but I couldn’t get to the label properties.

There isn’t much out there for this topic however after a bit of goggling I found that there is a new js library available


in SP2013. I just added the script link from 15 hive on my page and got some functions. I was only interested in label property but there are wssID , TermGuid etc also available. To get to the label property I used following:

oListItem.get_item(' News_x002d_Information_x002d ').get_label()

I hope this post will save you a bit of time and in time of pressure you can concentrate on more important things.

Happy SharePointing J would love your comments.
Alternate way:
UPDATED 27/02/2013 {
Guys , i found another method to get the label of the taxonomy field WITHOUT using Sp.Taxonomy.Js file.
Really easy
First wrap the oListItem.get_item(' News_x002d_Information_x002d ') in JSON.stringify() method like this and get an alert.
alert(JSON.stringify(oListItem.get_item(' News_x002d_Information_x002d ')))
This will display the information stored in the [object]. example:
by looking at the above image. You can see that you need to call 'Label' to get the details. Therefore just append that at the end.
oListItem.get_item(' News_x002d_Information_x002d ').Label


Omer Zubair

would like to thank the post 

for the help.

No comments:

Post a Comment