1

I get the following error:

"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" .

when attempting to set the permissions of a list item with the following code:

 if (!listitem.HasUniqueRoleAssignments) 
 { 
    listitem.BreakRoleInheritance(false);//I got an error here 
 }

How can I correct this error?

1 Answer 1

3

What is calling BreakRoleInheritance? Let's say it is an Event Receiver, but whatever it is, you need to make sure that the user has adequate permissions. Just because a user can add or update items in a list, doesn't mean that user has rights to call BreakRoleInheritance. For that, the user must have Manage Permissions rights.

To ensure the proper permissions, it is safest to call BreakRoleInheritance in the context of the site collection system account:

if (!listitem.HasUniqueRoleAssignments) 
{ 
    Guid siteId = listitem.Web.Site.ID; 
    Guid webId = listitem.Web.ID;
    Guid listId = listitem.ParentList.ID;
    int itemId = listitem.ID;
    SPUserToken token = listitem.Web.Site.SystemAccount.UserToken;
    using (SPSite site = new SPSite(siteId, token))
    {
        using (SPWeb web = site.OpenWeb(webId))
        {
            SPList list = web.Lists[listId];
            SPListItem item = list.GetItemById(itemId);
            item.BreakRoleInheritance(false);
        }
    }
}

A couple other things to check:

  1. If you are using elevated privileges, set allow unsafe updates to true.
  2. If versioning is enabled, be sure the item is checked in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.