Using JSOM to query SharePoint 2013 lists with Taxonomy Fields

So, I’d an interesting problem. I have a SharePoint 2013 list that uses a Managed Metadata (or ‘Taxonomy’) field. I need to, through the JavaScript Client Object Model, get all items that have a particular value or a child of that value on the taxonomy tree. As an example, here’s my taxonomy:termSet Sounds like it should be simple to query this, right? Well, not so much. Continue reading “Using JSOM to query SharePoint 2013 lists with Taxonomy Fields”

Using JSOM to query SharePoint 2013 lists with Taxonomy Fields

Issues with getting the SP.ClientContext in SharePoint 2013

So, I’m migrating a solution from SP2010 to 2013. In the JavaScript for part of the solution, I use the JavaScript client-side object model (JSOM). This involves getting the current ClientContext for the site:

var clientContext = new SP.ClientContext.get_current();

Unfortunately, ClientContext was null, so I kept getting Null Reference exceptions.

“Weird”, I thought, “This worked in 2010”. Now, my solution has a very similar piece of code elsewhere in it, so I went and tested it – and it worked. Even weirder.

Next up, I tried using a SP.SOD.executeOrDelayUntilScriptLoaded() call around the code where I got the ClientContext. Surely it was just a matter of waiting for the JavaScript file ‘sp.js’ to load? Well, it never loaded. With these two lines…

SP.SOD.executeOrDelayUntilScriptLoaded(function () { alert('SP.Runtme.js Loaded'); }, 'SP.Runtime.js');
SP.SOD.executeOrDelayUntilScriptLoaded(function () { alert('SP.js Loaded'); }, 'SP.js');

…no alerts were shown. On the page that works, they both get shown nearly instantly.

Okay, so it’s not my code then, it’s that the JSOM isn’t initializing, but only on one particular page. That sucks. What can I do to initialise it? Well, I found this post describing the same problem, but for publishing pages. Mine isn’t – it’s an application page – but I suspected it might be the same issue. And I checked MSDN which describes initialising sp.js.

I added this line:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { });

And my code began working again on this page. So it seems that for some pages in SharePoint 2013 the Client Object Model isn’t necessarily initialized.

Issues with getting the SP.ClientContext in SharePoint 2013

Check Permissions in JavaScript Client Object Model

I had a need to check the rights a user had on a particular item in SharePoint. Unfortunately, this had to be done entirely client side. Naturally, I turned to the client object model – but it took a little time to figure out.

I wanted to query for a particular item, some (though not all) of it’s properties, and I wanted to get it’s permissions. This post by Henrik Andersson gave me a good clue, though it didn’t explicitly mention getting the properties. To get the item, with it’s properties and it’s permissions:

//var clientContext = new SP.ClientContext(.... ;
clientContext.load(listItem, 'EffectiveBasePermissions', 'ID', 'Title', 'Owner', 'Active', 'Modified', 'Editor');
clientContext.executeQueryAsync(Function.createDelegate(this, this.OnItemQueryCompleted), Function.createDelegate(this, this.OnItemQueryFailed)); 

That performs the query to get the item and permissions – but how do you check them? Well, you need to to use the SP.ListItem.get_effectiveBasePermissions() method. (Note: there are SP.List.get_effectiveBasePermissions() and SP.Web.get_effectiveBasePermissions() methods too, for those tiers of the hierarchy).

function OnItemQueryCompleted(sender, args) {
	var perms = listItem.get_effectiveBasePermissions();

	if (perms.has(SP.PermissionKind.editListItems)) {
	      // ....

The permissions are retrieved into an SP.BasePermissions object, which has the .has() method that you can use to check the permissions (returns true if the user does have that permission). The Permissions mask is defined by the values in the SP.PermissionKind enumeration.

Check Permissions in JavaScript Client Object Model

Handle Exceptions in Client Object Model callbacks

A note to myself, of something I found on MSDN – for the ‘failed’ function on an asynchronous JavaScript call in the SharePoint Client Object Model:

function OnItemQueryFailed(sender, args)
   alert('Error:n' + args.get_message() + 'n' + args.get_stackTrace());

Note that these details are from the SP.ClientRequestFailedEventArgs class. There are other properties too.

Handle Exceptions in Client Object Model callbacks

Error: 'b' is null or not an object

This error has caused me so much pain when trying to use SharePoint’s JavaScript client-side object model (CSOM), so, in case I have it again:

  1. Check that the function exists.
  2. Check that the function is named correctly.
  3. Make sure that you’re not using “this” in the call to createDelegate, as detailed here.
    • Right: Function.createDelegate(this, onSuccessMethod)
    • Wrong: Function.createDelegate(this, this.onSuccessMethod)

    I don’t know why this difference should cause this error, but I’ve proved it true.

If you know of other causes of this error, let me know, I’ll add it to the list. I love the experience of debugging JavaScript…

Error: 'b' is null or not an object