SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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