I am facing error "Access is denied" on "item.BreakRoleInheritance" while login with restricted user permission. Although i am using it under
SPSecurity.RunWithElevatedPrivileges(
delegate()
{});
Please correct me what i am doing wrong as i've spent alot of time surfing google and found many solutions but nothing work out. Here is my method;
public void RemoveAllPermissions(SPItemEventProperties properties)
{
SPList myList = properties.Web.Lists[properties.ListId];
//Guid listID = properties.ListId;
//Guid listItemID = properties.ListItemId;
SPListItem listItem = myList.Items.GetItemById(properties.ListItemId);
//properties.Web.Lists[listId].GetItemById(itemId);
SPUserToken token = properties.ListItem.Web.Site.SystemAccount.UserToken;
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
using (SPSite site = new SPSite(listItem.Web.Site.ID, token))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(listItem.Web.ID))
{
web.AllowUnsafeUpdates = true;
//SPWebApplication webApp = web.Site.WebApplication;
//webApp.FormDigestSettings.Enabled = false;
listItem.BreakRoleInheritance(true); /// Error: Access is denied
web.AllowUnsafeUpdates = true;
//Get the list of Role Assignments to list item and remove one by one.
SPRoleAssignmentCollection SPRoleAssColn = listItem.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
{
SPRoleAssColn.Remove(i);
}
//webApp.FormDigestSettings.Enabled = true;
web.AllowUnsafeUpdates = false;
}
}
});
}
And calling it in event receiver method ItemUpdated() like
userRoleAndPermission = new UserRoleAndPermission();SPWeb currentWeb = properties.ListItem.Web;
currentWeb.AllowUnsafeUpdates = true;
userRoleAndPermission.RemoveAllPermissions(properties);
But getting error "Access is denied". Please pin point what i m doing wrong.