views:

830

answers:

2

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.

A: 

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.

Boris
A: 

I am trying similar with ECMAScript:

function Initialize() {
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        clientContext.load(web);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
    }

 function isUserWebAdmin() {
        var permissionMask = null;
        permissionMask = new SP.BasePermissions();
        permissionMask.set(SP.PermissionKind.manageWeb);
        var result = new SP.BooleanResult();

        result = web.doesUserHavePermissions(permissionMask);
        alert(result.get_value())

    }

It always returns 0.

Any thoughts?

Donaldinio
1. After the line result = web.doesUserHavePermissions(permissionMask); add clientContext.executeQuery(); I think that you won't be able to get the actual result unless you execute the client context query. 2. If the previous didn't change anything, try changing the manageWeb enum to different values to see if you'll get 0 for those as well. Maybe the correct result is in fact 0.
Boris

related questions