Monday, 7 May 2012

Adding SharePoint group to the site programmatically


Hello,

We had a requirement like when one of the sub site gets created, permission inheritance should be broken and new group with specific permission level should be added.

Here I will discuss the steps to break the inheritance and adding custom SharePoint group with custom permission level.  We write one feature and included in our custom site definition for the given sub site. We did our code in FeatureActivated event.
  1.  Allow unsafe update on the web, this can be done as follows

//get a reference to the web
SPWeb web = properties.Feature.Parent as SPWeb;

//get a current value of AllowUnsafeUpdates property
bool allowunsafeupdate = web.AllowUnsafeUpdates;

//Allow the unsafe updates
web.AllowUnsafeUpdates = true;

2     Break the inheritance permission, this can be done by calling BreakRoleInheritance() method as follows

//break the inheritance permission and not copying the role assignment from parent
web.BreakRoleInheritance(false);
  
Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.breakroleinheritance.aspx

The false argument indicates not to copy the roles assignment from parent object.

Now to add the group to current web, there is a property AssociatedGroups which returns list of SPGroup instance and then using Add() method we can add the group. This Add() method requires instance of SPGroup.

So to get the instance of SPGroup which we need to add, we need to go through the each group for root web(or parent web) and compare with which we need to add like as follows

//My custom group name
string myCustomGroupName = “MyCustomGroupName”;

//Getting all associated groups from root web
IList<SPGroup> associatedGroups = web.Site.RootWeb.AssociatedGroups;

//My custom group declaration
SPGroup myCustomGroup = null;

//Looping through all the groups to get my custom group instance
foreach(SPGroup group in associatedGroups){


//Comparing mycustom group name with current group name
if(group.Name.Equals(myCustomGroupName, StringComparison.InvariantCultureIgnoreCase)){
myCustomGroup = group;
break;
   }//if-end           
}//foreach - end

if(myCustomGroup!= null){
       //Adding my custom group to the group collection of web
web.AssociatedGroups.Add(myCustomGroup);
}

Now to assign the custom permission level to my custom group

//Getting my custom role(custom permission level created)
SPRoleDefinition customPermissionRole = web.RoleDefinitions["CustomPermission", web.Site.RootWeb)];

//Creating role assignment object for my group  
SPRoleAssignment customRoleAssignment = new SPRoleAssignment(myCustomGroup);

//Adding custom permission level to the roledefinitions bindings
customRoleAssignment.RoleDefinitionBindings.Add(customPermissionRole);

web.RoleAssignments.Add(customRoleAssignment);

3.      Update the web and reset the AllowUnsafeUpdates property

web.Update();
web.AllowUnsafeUpdates = allowunsafeupdate;

Any comments / suggestions are welcome J
Hope this will help you! Enjoy reading!

Thanks!

2 comments:

Anonymous said...

very nice..thanks

joy said...

I love your blog. Keep it up.Visit my site too.

n8fan.net

www.n8fan.net