Kategoriarkiv: Managed Navigation

Sharepoint taxonomy

How to set a custom property of a Taxonomy term set using JSOM in SharePoint 2013

(If you are lazy and just want to see the entire script, it’s on GitHub. Don’t bother reading all these words I wrote..)

I was trying to configure a term set I created to be used for navigation, using JavaScript. The documentation on this is not extensive, so I thought I’d share it.

Defining a term set

This is done by loading the current context, getting a taxonomy session and the creating your term group and set:

For this to work you have to have the SP.Taxonomy loaded on your page. I chose to insert it in my master page, but if you can’t do that, there are other ways.

Setting the custom property

The line that actually set the custom property looks like this:

This method is buried so deep on MSDN that I originally couldn’t find it at all. It’s described on the Term class, but not, as far as I can see, on the term set class. setCustomProperty takes a property and the value of that property. A custom property can also be something that you create yourself, and in this blog post is a guide to where to find them.

Creating the term set in the term store

What you have done in the previous step is not actually creating anything in the term store. You have only created a query or task that should be executed at a later time. The actual execution is done in two steps (and this is semantically a bit unintuitive, in my opinion):

That executes your query and creates the term set in your default term store. context.executeQueryAsync takes to callbacks, the first one is executed if the query was successfull, the other if it fails.

And this is where we run the risk of ending up in callback hell. To be able to work with the objects we create, we often need to do this within the previous callback. In this cas, this is not an issue, but say we have to work with the term set, for example creating our own navigation, then this can only be done inside the callback, since this is the only place where we have access to the term set.

Creating terms with URL:s

Creating a term is as easy as creating a term set. You just use your term set and add a term to it:

Where the parameters are: name, language code and GUID. By using similar syntax as above we can also set a URL to the term:

This will create a term that leads to the root och your domain.

Putting it all together

We want to create an entire script of this! In my finished script I also delete the term set if it already exists, but it’s too long to fit here!

And there you have it! If you want to look at my complete script, it’s on GitHub.