Windows SharePoint Services manages permissions through role definations. SPRoleDefinition and SPRoleDefinition classes provides methods to assign users to roles. Following code snippet shows how to assign the Contributor role to a custom SharePoint group programatically:

using (SPSite site = new SPSite(siteCollection))
{
using (SPWeb subWeb = site.OpenWeb(siteName))
{
//You need to break role inheritence if you want to assign unique permission to subsite
if (!subWeb.HasUniqueRoleAssignments)
subWeb.BreakRoleInheritance(true);
SPRoleDefinition roleDefination = parentWeb.RoleDefinitions["Contrubute"];
SPRoleAssignment roleAssignment = new SPRoleAssignment("MyCustomerGroup");
roleAssignment.RoleDefinitionBindings.Add(roleDefination);
subWeb.RoleAssignments.Add(roleAssignment);
subWeb.Update();
}
}


Users or Groups can be assigned permission to List or Document library or list item. Following code shows assigning permission on Folder of a document library for a custom sharepoint group:


using (SPSite site = new SPSite(siteCollection))
{
using (SPWeb web = site.OpenWeb(siteName))
{
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists[libraryName];
//Get folder
SPListItem item = docLib.Folders[1];
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(true);
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
SPRoleDefinition roleDefination = web.RoleDefinitions["Contribute"];
roleAssignment.RoleDefinitionBindings.Add(roleDefination);
item.RoleAssignments.Add(roleAssignment);
item.Update();
}
}
3

View comments

This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.Learn MoreGot it
    Loading