In this blog post I’m gonna show you how to use the OOB Taxonomy picker control without any server side control or code, just using JavaScript.

As you probably know, the Taxonomy picker control is the TaxonomyWebTaggingControl class, that looks like:

image image

However, all the core functionality of this control is actually done by JavaScript so, in some cases, can be useful how to reuse all of this functionality without any server side stuff, for example, imagine you can add a Taxonomy picker control using a Script editor web part, letting the user pick a Term and then execute some custom JS code.

If someone is curious, I got all this knowledge with some reflection from the TaxonomyWebTaggingControl class, and some hours reading a lot of JS code that SharePoint generates when you work with Taxonomy fields (no magic Smile)

First thing you need to know is that almos all of the Taxonomy picker control functionality is inside the Script “scriptforwebtaggingui.js”, and if you are working with Taxonomy, likely you’ll also need the file “sp.taxonomy.js”, so, ensure you have a reference to this files an option to this could be using the ScriptLink, if you can edit the MasterPage:




or, you can use the SP.SOD framework. Here is the JS code generated by the first ScriptLink above:








From HTML code, the Taxonomy picker control needs 2 html tags:

  • One tag is a hidden tag, and will save the selected terms in the format: Label|Term_GUID;OtherLabel|TermGUID…
  • The other tag is a DIV container, where JS code will inject the control, the show picker image, autocomplete options, and so on



Now, you need to Initialize the control from more JavaScript code. As you likely will need the Taxonomy JS code and a SharePoint context, this is a good way to ensure all the JS files needed are loaded first:






Then, in the Init function, let’s initialize the Taxonomy picker control:
















































































Most of the params are self-explained, and also are the same params in the TaxonomyWebTaggingControl class. Interesting params are:

  • taxControlContentId: this is the ID from the DIV container in the HTML I showed you above.
  • InputFieldId: ID of the Hidden control.
  • SspId: This is the TermStore ID. I’ll show you later how to get that ID from more JS code.
  • termSetId: Term set ID that you want to bind the control. You can get it using sp.taxonomy.js, for example, get the ID from the TermSet name.

Here is the code to get the Term Store ID (SspId):


































Finally, let’s say you wanna set a value before loading the control, for example, you get the Field value or a User profile Taxonomy property, and you want to set the control with that value. In this case, you just have to set the Hidden control value, like:


You have to do that before configuring the control, before calling the:


and that’s all ! you will get all the Taxonomy picker functionality and styling (obviously, you have to manage what you wanna do with the selected values, the loading and saving functionality is not done by the control)

Last note

If you’re using SharePoint apps and want to use the Taxonomy picker, you can use the code from AMS Office code, where Richard diZerega has made a great job creating a JS control with almost same functionality that the OOB Taxonomy picker control.

Also, that code can be used inside SharePoint, as is using just jQuery and SP JSOM. I did it and worked fine. However, that control has some lacks comparing with the OOB, like for example, you can’t use the keyboard to select the Terms, only the mouse (users are use to type, use the arrows, and enter tab to select a Term). This can’t be done with the code sample (unless I did some wront, didn’t worked that way to me). Of course, this is some sample code, and you have the entire source to adapt it to your needs.

Hope it helps!

Luis Manez
@luismanez