Friday, August 29, 2008

Break the permissions inheritance of a site

This code allows you to break the permissions inheritance of a site

string siteGroupNamePublisher = "SampleReadOnly";
string desc = "A group to manage read only user for root site";
siteCol = (SPSite)properties.Feature.Parent;
site = siteCol.RootWeb;
try {
SPSecurity.RunWithElevatedPrivileges(delegate() {
//Start--Creating SampleGroup which has read permissions n Break Inheritense
logFile.WriteInfo(CLASS, "FeatureActivated", "Creating SampleGroup which has read permissions n Break Inheritence", debugInfo);
SPGroup secGroup1;
SPRoleAssignment roleAssignment1;
SPRoleDefinition roleDefinition1;
site.SiteGroups.Add(siteGroupNamePublisher, site.CurrentUser, site.CurrentUser, desc);
secGroup1 = site.SiteGroups[siteGroupNamePublisher];
roleAssignment1 = new SPRoleAssignment(secGroup1);
roleDefinition1 = site.RoleDefinitions["Read"]; roleAssignment1.RoleDefinitionBindings.Add(roleDefinition1); site.RoleAssignments.Add(roleAssignment1);
//End--Creating SampleGroup which has read permissions n Break Inheritense
});

No comments: