Requirement:
I need to remove user permission from list item based on some specified condition; normal user don’t have rights to remove user level permission, so I need to put my business logic into RunWithElevatedPrivileges delegate into event receiver class, but I was facing some challenges after that also, normal user was getting access denied errors while breaking inheritance of list item into event receiver class.
Solution:
We need to be very careful while writing impersonation code into event receiver, because if we miss one class or object to take reference from current logged in user then code won’t work, in my case – I was taking List Item on current logged in user context, I need to be very specific for taking List Item. Key here is needs to take list item by SPListItem objItem = elevatedWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID);
SAMPLE CODE:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(properties.SiteId))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl))
{
SPListItem objItem = elevatedWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID);
objItem.Web.AllowUnsafeUpdates = true;
objItem.BreakRoleInheritance(false);
objLeaseItem.Web.AllowUnsafeUpdates = true;
}
}
});
Reference Links:
http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/f2ccd61a-8828-4c17-8360-20d45d6b9514
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c3d2b304-7fcc-40d2-86ce-61d9b21b03d7
http://boris.gomiunik.net/2009/04/spsecurityrunwithelevatedprivileges-and-access-denied-error-on-event-receiver/
Good Luck!
Friday, December 11, 2009
How to impersonate user into event receiver class or Access denied error on event receiver
Labels:
Access denied error on event receiver,
access denied errors into event receiver class,
access denied errors while breaking inheritance of list item,
event receiver,
event receiver class,
How to impersonate user into event receiver class,
How to impersonate user into event receiver class or Access denied error on event receiver,
MOSS 2007,
Personal Experience,
properties.ListId,
properties.ListItem.ID,
properties.RelativeWebUrl,
properties.SiteId,
remove user permission from list item,
RunWithElevatedPrivileges,
SharePoint,
SharePoint Event Receiver,
SharePoint Features,
Sharepoint Problems,
user level permission
Subscribe to:
Post Comments (Atom)
Thanks for this code sample. Worked great. I did not have to specify objItem.Web.AllowUnsafeUpdates = true
ReplyDelete