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.

Advertisements
Check Permissions in JavaScript Client Object Model

5 thoughts on “Check Permissions in JavaScript Client Object Model

  1. Hello. Thank you for the example. I tried to get this to work but am nvaihg some problems.1. I downloaded the jquery ui and saved the js files to a shared library2. I changed the reference to the first set of code to match where the files are saved.3. I put the first block of code into a content editor webpart on the home page of my site4. I copied the rest of the code taking out the numbers in front of each line and saved it as ecmatest.js and uploaded to the same document library as the other js files5. When the site runs keep getting an error that it is expecting an }’Can you please tell me if what I did was initially correct. Also is it possible to get your css file.

    Like

  2. @Dion, well … the code I was using above doesn’t use jQuery. I’m not clear what you’re actually using that for. But from the error message you mentioned, I suggest you’re missing a closing bracket somewhere.

    Note that the example above is only snippets of code, not a complete solution.

    Like

  3. karthik says:

    Can you please provide me the entire code, bz I am using same functionality into my project, However I am not able to achive it.

    I have some custom tab in the ribbon on Custom List, when user has edit permission to the particualr listitem then I need to enable when user select the item check box. please share the code.

    thanks
    Karthik

    Like

    1. To be honest, that pretty much is the whole code. The only bits that are additional are kind of specific to what I was doing…

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s