none
Client object model to check the user role assignment in sharepoint 2010

    Question

  • hello,

     Please find the below code : i am trying to fetch the permission level name for the user using client object model.

    i am getting error as "Value does not fall within the expected range." on execute query call. How to handle this if the user having multiple roledefinition, since GetByPrincipal method return RoleAssignment not RoleAssignmentCollection. So i want to get the collection of role assignemnt to the user.

                ClientContext clientContext = new ClientContext(url);
                           Web oWeb = clientContext.Web;
               
                            Principal oUser = oWeb.EnsureUser("Domain\\User name"); 
                RoleAssignment oRoleAssignment = (oWeb.RoleAssignments.GetByPrincipal(oUser));
                   
                          clientContext.Load(oUser);
                       clientContext.Load(oRoleAssignment.RoleDefinitionBindings); 
                           clientContext.ExecuteQuery();


    regards Vimal

    Wednesday, October 03, 2012 11:31 AM

Answers

  • Try below code to retrieve permissions of the user but you have to convert the code to client object model.

    SPUser rajuser = web.EnsureUser("domain\\raj");
    SPPermissionInfo info = web.GetUserEffectivePermissionInfo(rajuser.LoginName);
    foreach (SPRoleAssignment roleAssignment in info.RoleAssignments)
    {
    	foreach (SPRoleDefinition role in roleAssignment.RoleDefinitionBindings)
            {
            	Console.WriteLine(role.Name);
    	}
    }

    Wednesday, October 03, 2012 8:34 PM
  • Hi Vimal,

    Please take a look at this code that prints the permission levels for a certain user (userName variable):

    using System;
    using System.Linq;
    using System.Net;
    using Microsoft.SharePoint.Client;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://perseus/";
            string userName = "PERSEUS\\dmitry.kaloshin";
            ClientContext clientContext = new ClientContext(url);        
            clientContext.Credentials = new NetworkCredential("Administrator", "12345");
    
            HashSet<string> permissionLevels = new HashSet<string>();        
            User usr = clientContext.Web.EnsureUser(userName);
    
            GroupCollection groups = clientContext.Web.SiteGroups;
            clientContext.Load(usr);
            clientContext.Load(groups);        
            clientContext.ExecuteQuery();
            
            foreach (var group in groups)
            {
                clientContext.Load(group, g => g.Users.Where(u => u.LoginName == usr.LoginName));            
                clientContext.ExecuteQuery();
    
                foreach (var user in group.Users)
                {
                    RoleAssignmentCollection roleAssignments = clientContext.Web.RoleAssignments;
                    clientContext.Load(clientContext.Web, w => w.RoleAssignments.Where(ra => ra.Member.LoginName == group.LoginName ||
                        ra.Member.LoginName == user.LoginName));
                    clientContext.ExecuteQuery();
    
                    foreach (var ra in clientContext.Web.RoleAssignments)
                    {
                        clientContext.Load(ra.Member);
                        clientContext.Load(ra.RoleDefinitionBindings);
                        clientContext.ExecuteQuery();
                        foreach (var definition in ra.RoleDefinitionBindings)
                        {
                            clientContext.Load(definition);
                            clientContext.ExecuteQuery();
                            permissionLevels.Add(definition.Name);
                        }                    
                    }
                }
            }        
            foreach (string s in permissionLevels)
            {
                Console.WriteLine(s);
            }
        }
    }



    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog

    Thursday, October 04, 2012 11:28 AM

All replies

  • Try below code to retrieve permissions of the user but you have to convert the code to client object model.

    SPUser rajuser = web.EnsureUser("domain\\raj");
    SPPermissionInfo info = web.GetUserEffectivePermissionInfo(rajuser.LoginName);
    foreach (SPRoleAssignment roleAssignment in info.RoleAssignments)
    {
    	foreach (SPRoleDefinition role in roleAssignment.RoleDefinitionBindings)
            {
            	Console.WriteLine(role.Name);
    	}
    }

    Wednesday, October 03, 2012 8:34 PM
  • Hi Vimal,

    Please take a look at this code that prints the permission levels for a certain user (userName variable):

    using System;
    using System.Linq;
    using System.Net;
    using Microsoft.SharePoint.Client;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://perseus/";
            string userName = "PERSEUS\\dmitry.kaloshin";
            ClientContext clientContext = new ClientContext(url);        
            clientContext.Credentials = new NetworkCredential("Administrator", "12345");
    
            HashSet<string> permissionLevels = new HashSet<string>();        
            User usr = clientContext.Web.EnsureUser(userName);
    
            GroupCollection groups = clientContext.Web.SiteGroups;
            clientContext.Load(usr);
            clientContext.Load(groups);        
            clientContext.ExecuteQuery();
            
            foreach (var group in groups)
            {
                clientContext.Load(group, g => g.Users.Where(u => u.LoginName == usr.LoginName));            
                clientContext.ExecuteQuery();
    
                foreach (var user in group.Users)
                {
                    RoleAssignmentCollection roleAssignments = clientContext.Web.RoleAssignments;
                    clientContext.Load(clientContext.Web, w => w.RoleAssignments.Where(ra => ra.Member.LoginName == group.LoginName ||
                        ra.Member.LoginName == user.LoginName));
                    clientContext.ExecuteQuery();
    
                    foreach (var ra in clientContext.Web.RoleAssignments)
                    {
                        clientContext.Load(ra.Member);
                        clientContext.Load(ra.RoleDefinitionBindings);
                        clientContext.ExecuteQuery();
                        foreach (var definition in ra.RoleDefinitionBindings)
                        {
                            clientContext.Load(definition);
                            clientContext.ExecuteQuery();
                            permissionLevels.Add(definition.Name);
                        }                    
                    }
                }
            }        
            foreach (string s in permissionLevels)
            {
                Console.WriteLine(s);
            }
        }
    }



    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog

    Thursday, October 04, 2012 11:28 AM