none
Item event receiver fires only for users with full control, not contributors

    Question

  • 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
    Tuesday, June 12, 2012 6:44 PM

Answers

  • 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
    Wednesday, June 13, 2012 4:36 PM

All replies