Monday, 7 May 2012

Adding SharePoint group to the site programmatically


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

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;
}//foreach - end

if(myCustomGroup!= null){
       //Adding my custom group to the group collection of web

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


3.      Update the web and reset the AllowUnsafeUpdates property

web.AllowUnsafeUpdates = allowunsafeupdate;

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



Anonymous said...

very nice..thanks

joy said...

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

Karthika Shree said...

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
Sharepoint Training in Chennai

Ray Norman said...

I'm often to blogging and i actually admire your content. The article has actually peaks my interest. I am going to bookmark your website and hold checking for new information. online casino real money