Reputation: 10234
The following code always returns false (which is incorrect, as the user has Full Control permission at the site level):
Site site;
BasePermissions permissionMask;
ClientResult<bool> result;
permissionMask = new BasePermissions();
permissionMask.Set(PermissionKind.ManageWeb);
result = site.DoesUserHavePermissions(permissionMask);
return result.Value;
I am trying to utilize new SharePoint 2010 Client Object Model. I was thrilled when I discovered DoesUserHavePermissions method, but it appears that I'm not really sure if I know how to use it. I have no idea whether I am using the correct mask, or whether I should specify the user account for which I wish to check the permissions level? Any help would be greatly appreciated. Thanks.
Upvotes: 3
Views: 10519
Reputation: 479
Just thought I'd add some code I'm using for this. Which is pretty much the same but without the bloat.
using (var context = new ClientContext(siteUrl))
{
context.Load(context.Web);
context.ExecuteQuery();
BasePermissions permissionMask;
ClientResult<bool> hasPermissions;
permissionMask = new BasePermissions();
permissionMask.Set(PermissionKind.ManageWeb);
hasPermissions = context.Web.DoesUserHavePermissions(permissionMask);
}
Upvotes: 1
Reputation: 10234
One important thing was missing - the Client Context. This object is responsible for the actual execution of the query over any SharePoint Client Object Model objects.
The code should be modified to the following:
ClientContext clientContext;
Site site;
BasePermissions permissionMask;
ClientResult<bool> result;
permissionMask = new BasePermissions();
permissionMask.Set(PermissionKind.ManageWeb);
//if we want to check ManageWeb permission
clientContext = new ClientContext(siteUri);
//siteUri is a method parameter passed as a string
clientContext.Credentials = credential;
//credential is a method parameter passed as a NetworkCredential object
//that is the user for which we are checking the ManageWeb permission
site = clientContext.Web;
result = site.DoesUserHavePermissions(permissionMask);
return result.Value;
This will return true if the user is assigned ManageWeb permissions, or false if otherwise. For a complete list of permissions enum, take a look at this MSDN page.
Upvotes: 4