Set permissions the 'Right-way' at SPListItem & SPWeb
There has always been a confusion between assigning custom permissions between a listitem and a sharepoint site.
Setting site level permissions programmatically at website level
Setting site level permissions programmatically at website level
Code :
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://site/subsite"))
{
using (SPWeb webSubSite = site.OpenWeb())
{
webSubSite.BreakRoleInheritance(false);
SPGroup group =
webSubSite.Site.RootWeb.SiteGroups["Group_Display_Name"];
SPRoleDefinitionCollection SubSiteWebRoleDefn =
webSubSite.RoleDefinitions;
SPRoleAssignment objRoleAssign = new SPRoleAssignment((SPPrincipal)group);
objRoleAssign.RoleDefinitionBindings.Add(SubSiteWebRoleDefn["Read"]);
webSubSite.RoleAssignments.Add(objRoleAssign);
webSubSite.Update();
}
}
});
Setting site level permissions programmatically at listitem level
Code :
Setting site level permissions programmatically at listitem level
Code :
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(currentItem.Web.Site.ID))
{
using (SPWeb web =
site.OpenWeb(currentItem.Web.ID))
{
web.AllowUnsafeUpdates
= true;
SPList elevatedmyList =
web.Lists[properties.ListId];
elevatedItem.BreakRoleInheritance(false);
elevatedItem.RemoveAllItemPermissions();
SPGroup AGOIntranetUsersGroup =
elevatedItem.Web.SiteGroups["Group_Display_Name"];
SPRoleDefinition AGOIntranetUsersGroupRole =
elevatedItem.Web.RoleDefinitions["Read"];
elevatedItem.SetPermissions(AGOIntranetUsersGroup,
AGOIntranetUsersGroupRole);
elevatedItem.SystemUpdate();
web.AllowUnsafeUpdates
= false;
}
}
});
Set permissions the 'Right-way' at SPListItem & SPWeb
Reviewed by
Srikant N
on
Thursday, October 26, 2017
Rating:
No comments :
Thanks for your time ! Have a great day.