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.
- Allow unsafe update on the web, this can be done as follows
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
Now to assign the custom permission level to my custom group
Any comments / suggestions are welcome J
Hope this will help you! Enjoy reading!