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.