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

Trying to use an Event Handler in SharePoint 2007 my test user was seeing this error:

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

The error was being triggered by an ItemUpdated or ItemAdded event.

fix:

Public Overrides Sub ItemUpdated(ByVal properties As SPItemEventProperties)
SPSecurity.RunWithElevatedPrivileges(AddressOf mCode)
End Sub

Private Sub mCode()
Dim eFire As hEventFiring = New hEventFiring
Using spsSite As SPSite = New SPSite(sPi.WebUrl)
Using spwWeb As SPWeb = spsSite.OpenWeb

Dim iSPl As SPListItem = spwWeb.Lists(sPi.ListId).GetItemById(sPi.ListItem.ID)

Try

eFire.dEventFire()
Dim roleAssig As New SPRoleAssignment(spwWeb.SiteGroups(mGroup))
roleAssig.RoleDefinitionBindings.Add(spwWeb.RoleDefinitions(“Contribute”))

If iSPl.HasUniqueRoleAssignments = False Then
iSPl.BreakRoleInheritance(True)
End If

For Each spra As SPRoleAssignment In iSPl.RoleAssignments
spra.RoleDefinitionBindings.RemoveAll()
spra.Update()
Next

iSPl.RoleAssignments.Add(roleAssig)
iSPl(“CommentField”) = mGroup
iSPl.Update()
eFire.eEventFire()

Catch ex As Exception
iSPl(“CommentField”) = ex.Message.ToString
iSPl.Update()
Finally
End Try
End Using
End Using
End Sub

Class hEventFiring
Inherits SPItemEventReceiver

Public Sub dEventFire()
Me.DisableEventFiring()
End Sub

Public Sub eEventFire()
Me.EnableEventFiring()
End Sub

End Class

————

What I’m doing:

1. Use RunWithElevatedPrivileges to call the code that updates the item.

2. Disable event firing with eFire.dEventFire

3. Break role inheritance using BreakRoleInheritance

4. Assign permissions using iSPl.RoleAssignments.Add(roleAssig)

5. Enable event firing eFire.eEventFire.

The key to this working is Dim iSPl As SPListItem = spwWeb.Lists(sPi.ListId).GetItemById(sPi.ListItem.ID)

Leave a Reply

Your email address will not be published. Required fields are marked *