开发者

SharePoint permissions for a specific group

开发者 https://www.devze.com 2023-01-31 09:20 出处:网络
I\'m trying to establish whether a specific group has Read access to a particular site collection. I have been trying for a day and a half but feel as if I have found three halves of different soluti

I'm trying to establish whether a specific group has Read access to a particular site collection.

I have been trying for a day and a half but feel as if I have found three halves of different solutions!

The code fragments I have so far are:

开发者_如何学Go
using (SPSite site = new SPSite(this.GenerateAbsoluteUri(moduleCode, academicYear)))
{
    using (SPWeb web = site.OpenWeb())
    {
        for (int i = web.SiteGroups.Count - 1; i >= 0; i--)
        {
            SPGroup group = web.SiteGroups[i];

            if (Regex.IsMatch(group.Name, theGroupImLookingFor))
            {

but then what?!

Most of my Google results tell me about roles but I don't know how to tie a role to a group.

Please help!


To assign permission to a user (account) or a SharePoint group there are some objects that we need to look at in a certain order. The first thing we need to do is get the the security principal that we want to assign the role to (SPUser or SPGroup). The next thing we need to do it get the actual permission (role) that we want to assign (ex: Read, Full Control etc…). Then we need to create a SPRoleAssignment object and on the constructor pass it in the SPUser or SPGroup (security principal) that we want to assign the permissions to. Now we need to add the role definition to the RoleDefinitionBindings collection of the role assignment object. Then we need to add the actual role assignment to the web (site) and update the web. Below is the full code lisitng.

// Create the site that contains our list
using(SPSite oSite = new SPSite("<<my site url>>"))
{
    // Open the web object
  using(SPWeb oWeb = oSite.OpenWeb())
  {

    // Get the group that we want to add the user to
    SPGroup oGroup = oWeb.Groups["<<group name>>"];

    // Get the role definition we want to assign ex: Full Control
    SPRoleDefinition oRole = oWeb.RoleDefinitions["<< role name>>"];

    // Create the role assignment object
    SPRoleAssignment oRoleAssignment = new SPRoleAssignment(oGroup);

    // Add the role definition to the role assignemnt. 
    // This will assign the specific permission to the security principal for this role          assignemnt.
    oRoleAssignment.RoleDefinitionBindings.Add(oRole);

     // Now we need to add the role assignment to the web
     oWeb.RoleAssignments.Add(oRoleAssignment);

    // Now update the web
    oWeb.Update();
   }
}


Heres snippets from my own code (Sharepoint 2010). Creating a role:

SPRoleDefinition network_role = new SPRoleDefinition();
                network_role.BasePermissions = SPBasePermissions.AddListItems |
                    SPBasePermissions.BrowseDirectories |
                    SPBasePermissions.EditListItems |
                    SPBasePermissions.DeleteListItems;
                network_role.Name = "Network - Project Member";
                network_role.Description = "Provides permissions required for a member of a project.";

                web.RoleDefinitions.Add(network_role);

Adding a role to a group:

var assign = new SPRoleAssignment(oweb.SiteGroups["Network Project - " + item.Code]);
assign.RoleDefinitionBindings.Add(network_role);
0

精彩评论

暂无评论...
验证码 换一张
取 消