Quantcast
Channel: Brisbaneite
Viewing all articles
Browse latest Browse all 2

Copy a SharePoint 2007 group along with its associated permissions

$
0
0

One of the challenges I’ve quite recently faced is how to copy a SharePoint 2007 role group whilst also retaining all of the permissions associated with that group (case example: One job title is splitting into two but both titles are going to hold the same SharePoint access).

SharePoint security group permissions are stored as role assignments on the SPWeb object. So, first we must find the group we want to copy by using:

SPGroup group = spWeb.Groups["name group"];

Then we must use this retrieved group to get the roleassignments on the SPWeb object:

SPRoleAssignment ass = spWeb.RoleAssignments.GetAssignmentByPrincipal(group2);

Then we must simply create a new SPGroup and add the group to the roleassignment and the roleassignment to the web object:

spWeb.SiteGroups.Add(groupName, user, user, groupDescription);
SPGroup newGroup = spWeb.SiteGroups[groupName];
SPRoleAssignment roleAssignment = new SPRoleAssignment(newGroup);

//add role to web
spWeb.RoleAssignments.Add(roleAssignment);
spWeb.Update();

After this we should have a new group with the same permissions as the original group.

If you’re not doing the above in a SharePoint feature you can do it from a console application. Just create a console application in VS and fill it with something like this:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.SharePoint;   
namespace ConsoleApplication 
{     
class Program     
{         
static void Main(string[] args)         
{             
    using (SPSite spSite = new SPSite("http://yoururl"))            
    {                 
          using (SPWeb spWeb = spSite.RootWeb)   
          {                     
            //perform the code to clone the group here
          }          
    }                  
 }    
 } 
}

 


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images