다음을 통해 공유


Associate Permission Levels with SharePoint User Groups Using the Client Side Object Model in .NET/C#

Introduction

In the [[articles:How to get SharePoint user group names in a .NET/C# client application using SharePoint client object model|previous article]] we discussed about how to get the SharePoint user group names using SharePoint client object model from a client application (windows or web). We can consider this article as a continuation of the previous one. In this article, we will discuss about getting permission levels associated with one or all SharePoint User Groups.

Here, we are going to discuss two variants of the approaches. First one is to get all groups' permission information and in the second one we will do it selectively. In most of the real situations, developers need to get it for selective groups. For the details of the namespaces we need to include, please refer the [[articles:How to get SharePoint user group names in a .NET/C# client application using SharePoint client object model|previous article]].

Get all SharePoint User Groups' Permission Levels

This is not required if you are not really going to work with all groups in the given SharePoint site. Depending on the scenario, it may put lot of data in the wire unnecessarily. But still, if this is unavoidable under a given circumstance, please go ahead.

string url ="http://mysharepointsite.net"; // put your site url here.
using (ClientContext ctx = new ClientContext(url))
{
    #if DEBUG
    //Use the following line if you wish to connect to sharepoint site using 
       //a different credential than what the application is running under.
      ctx.Credentials = new  System.Net.NetworkCredential("myaccount", "mypass",  "mydomain");
    #endif
    Web wsite = ctx.Web;
    ctx.Load(wsite, w => w.HasUniqueRoleAssignments, w => w.RoleAssignments.Include(roleAssigned => roleAssigned.Member.Title,
            roleAssigned => roleAssigned.RoleDefinitionBindings.Include(
            roleDef => roleDef.Name)));
 
    ctx.ExecuteQuery();
    RoleAssignmentCollection rac = wsite.RoleAssignments;
    if (rac != null && rac.Count !=0)
    {       
        Console.WriteLine("Unique Permissions: " + wsite.HasUniqueRoleAssignments);
        foreach (RoleAssignment ra in rac) 
        {   
            Console.WriteLine("Group Name: " + ra.Member.Title);
            foreach (RoleDefinition rd in ra.RoleDefinitionBindings)
                Console.WriteLine("Permission: " + rd.Name);                
        }
    }
}

Get selective SharePoint User Group's Permission Levels

This is a targeted implementation. You are free to improvise based on your requirement. This code is going to get the permission information of the group name thisGroup only. This is sleek and generates optimal network traffic. The specific statement responsible for this is highlighted in the code sample given below.

string url ="http://mysharepointsite.net"; // put your site url here.
string spgName = "thisGroup"; //Get permission for the group thisGroup only
 
using (ClientContext ctx = new ClientContext(url))
{
    #if DEBUG
        //Use the following line if you wish to connect to sharepoint site using 
        // a different credential than what the application is running under.
        ctx.Credentials = new  System.Net.NetworkCredential("myaccount", "mypass",  "mydomain");
    #endif
    Web wsite = ctx.Web;
    ctx.Load(wsite, w => w.HasUniqueRoleAssignments, w => w.RoleAssignments.Include(roleAssigned => roleAssigned.Member.Title,
            roleAssigned => roleAssigned.RoleDefinitionBindings.Include(
            roleDef => roleDef.Name)).Where(roleAssigned => roleAssigned.Member.Title == spgName));
 
    ctx.ExecuteQuery();
    RoleAssignmentCollection rac = wsite.RoleAssignments;
    if (rac != null && rac.Count !=0)
    {        
        foreach (RoleAssignment ra in rac)                            
                foreach (RoleDefinition rd in ra.RoleDefinitionBindings)
                    Console.WriteLine("Permission: " + rd.Name);
    }
    else
        Console.WriteLine("'" + spgName + "' group not found.");
}

References

Applies To

  • SharePoint 2010
  • SharePoint 2013
  • .NET 3.5 and above