Item event receiver fires only for users with full control, not contributors
-
Tuesday, June 12, 2012 6:44 PM
I have an event receiver that works fine when I add an item or when someone else with full control adds an item. But when other users with Contribute permissions add an item, it does not fire, or at least it does not fully execute. In a nutshell, the code breaks inheritance on the item and sets permissions explicitly to a certain group and two individuals. Again, it DOES work for me and others with full control, but for a user with Contributor access, for example, it does not fire. I'm going to see if my admin can check the logs tomorrow for any errors because the users do not get an error in the browser:
Here is my code snippet (sanitized where appropriate):
public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); try { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(properties.SiteId)) { using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl)) { web.AllowUnsafeUpdates = true; SPListItem myItem = properties.ListItem; //This line detaches the item security from the list myItem.BreakRoleInheritance(false); this.DisableEventFiring(); myItem.Update(); SPMember member = web.SiteGroups["Management Owners"]; SPPrincipal principal = (SPPrincipal)member; SPRoleDefinition roledefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor); SPRoleAssignment myRoleAssignment = new SPRoleAssignment(principal); myRoleAssignment.RoleDefinitionBindings.Add(roledefinition); myItem.RoleAssignments.Add(myRoleAssignment); string managerstring = myItem["Manager Account Id"].ToString(); SPUser manager = web.EnsureUser(managerstring); myItem["Manager"] = manager; SPPrincipal managerprincipal = (SPPrincipal)manager; SPRoleDefinition managerroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader); SPRoleAssignment ManagerRoleAssignment = new SPRoleAssignment(managerprincipal); ManagerRoleAssignment.RoleDefinitionBindings.Add(managerroledefinition); myItem.RoleAssignments.Add(ManagerRoleAssignment); string associatestring = myItem["Author"].ToString(); int userid = Convert.ToInt32(associatestring.Substring(0, associatestring.IndexOf(";#"))); SPUser associate = web.AllUsers.GetByID(userid); SPPrincipal associateprincipal = (SPPrincipal)associate; SPRoleDefinition associateroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader); SPRoleAssignment AssociateRoleAssignment = new SPRoleAssignment(associateprincipal); AssociateRoleAssignment.RoleDefinitionBindings.Add(associateroledefinition); myItem.RoleAssignments.Add(AssociateRoleAssignment); myItem.Update(); this.EnableEventFiring(); //Handle messaging StringDictionary dict = new StringDictionary(); dict.Add("to", manager.Email); dict.Add("from", "us@usfolks.com"); dict.Add("subject", "bla bla bla"); string msgbody = "my message body"; SPUtility.SendEmail(web, dict, msgbody); } } }); } catch (Exception ex) { } }Any ideas?
- Edited by Dasani2008 Tuesday, June 12, 2012 7:36 PM
All Replies
-
Wednesday, June 13, 2012 5:14 AM
The event receiver should fire when the users with contribute permission added an item. Here with your code it seems the code you used in the added event should break this while executing the code using the contribute permission user. Just try this same code from any console application and make sure it is working for the contibute permission user. But you should face this breaking issue in the console too while using it with the contribute permission user. So do the required changes and then add it to the event receiver.
-
Wednesday, June 13, 2012 6:55 AM
Have a look at this threads:
- Get ListItem in ItemAdded event handler with elevated permissions
- Specifying Item Level Permissions using Sharepoint List Event Handler but to a list on which user has Contribute rights
Or check out the following blog post:
Mathias Aebischer
- Edited by Mathias Aebischer Thursday, June 14, 2012 11:47 AM broken link
-
Wednesday, June 13, 2012 3:14 PMThanks Mathias. That 2nd link is broken. It sounds like a good read though. Can you confirm?
-
Wednesday, June 13, 2012 4:36 PM
Actually, I found the answer in another thread.
One line fixed it:
SPListItem myItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID);
Thanks to "msaradhi" from this post for leading me to the answer.
- Proposed As Answer by Moonis Tahir Wednesday, June 13, 2012 4:49 PM
- Marked As Answer by Dasani2008 Tuesday, June 19, 2012 6:02 PM
-
Thursday, June 14, 2012 11:49 AMJust fixed the link in case you still care. Glad you were able to fix your problem.
Mathias Aebischer

