15 December 2015

SharePoint 2013 - GetUserEffectivePermissions returning incorrect results

Recently I was trying to get effective permission of a SharePoint user using client context (client side object model). I was trying to achieve this in SharePoint 2013 Provider Hosted App.

The method was executing sucessfully but when I was iterating through the effective permissions, I was getting false for all the permissions. Here is the sample code that I was using -

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

using (var clientContext = spContext.CreateUserClientContextForSPHost())
{

    string userName = @"domain\username";

    var userPermissions = clientContext.Web.GetUserEffectivePermissions(userName);
    clientContext.ExecuteQuery();

    foreach (var permission in Enum.GetValues(typeof(PermissionKind)).Cast<PermissionKind>())
    {
        var permissionName = Enum.GetName(typeof(PermissionKind), permission);
        var hasPermission = userPermissions.Value.Has(permission);
        Response.Write(String.Format("<br>Permission: {0}, HasPermission: {1}", permissionName, hasPermission));
    }


}

Finally I realized that my SharePoint application is claims enabled. So I have to pass user-name in claims format.

string userName = @"i:0#.w|domain\username";

The code worked like a charm after that.

Hope this helps someone as I spent quite some time in figuring out this small thing. Its not mentioned anywhere in MSDN forums. I have tried to answer this on few posts in stack-exchange and stack-overflow forums.


2 comments:

Anonymous said...

It does not work

Anonymous said...

it gives following
Error: Operation is not valid due to the current state of the object