månadsarkiv: november 2013

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.

Count lines of code with PowerShell

Today I got a question:

How many lines of code are there in our SharePoint solution?

After a little search, I found that PowerShell is really a nice tool to count lines of code:

I wanted to count lines for different types of code:

  1. Code Behind written in C#, the files have .cs file extension
  2. JavaScript code (except jQuery, angular or knockout frameworks)
  3. PowerShell files (.ps1 and psm1)
  4. Xml files (all the SharePoint .xml files)

Here is the powershell code that counts lines of code:

# go to the solution folder
cd <solution directory>

#count lines in .cs files
ls -include *.cs -recurse | select-string . | measure | select count

#count lines in our .js files
ls -include *.js -recurse `
    -exclude *min.js, jquery*, _*, jsrender*, CamlBuilder*, knockout* `
  | select-string . `
  | measure `
  | select Count

#count lines in our powershell scripts
ls -include *.xml -recurse | select-string . | measure | select count

#count lines in our powershell scripts
ls -include *.ps1, *.psm1 -recurse | select-string . | measure | select count

Just a curious fact, I can’t tell you how many lines of code we have in our solution, but I can reveal the proportions. If I used the flexible box model in css3, it would look like this:

lines of code

There are as many lines of code written in javascript as it is in C#. The main reason that for the big js code base are the SharePoint hosted apps. The PowerShell scripts are as big the javascript code base. Xml files are 4 times bigger than C# code, and it is even bigger than the sum of all lines of code written in C#, JavaScript and PowerShell. It isn’t strange that xml is dominating, almost everything in SharePoint is defined in xml.

Fortunately, there are less cases where you have to write raw xml in Visual Studio 2012/2013 and SharePoint 2013.

How does it look in your project? What language is dominating in your SharePoint project?

I really hope GoldieBlox becomes a thing

Doing the short and tiny thing again and giving a shout out to GoldieBlox, a toy that I hope will conquer the world. This is from their homepage:

At GoldieBlox, our goal is to get girls building. We’re here to help level the playing field in every sense of the phrase. By tapping into girls’ strong verbal skills, our story + construction set bolsters confidence in spatial skills while giving young inventors the tools they need to build and create amazing things.

Basically it’s a toy for girls that’s about creating and thinking to inspire them to do stuff themselves. Awesome!