none
Document library Event Receiver Deployment Issue

    Question

  • Hi

    I have a strange issue when I debug the event receiver, it works perfectly fine. However when I deploy and try to upload the file, it does not fire the event hander. Any help?

    Basically on item upload, I break the inheritance and add the owner with read permission and manager with contribute permission.

     public override void ItemAdded(SPItemEventProperties properties)
            {

                myWeb = properties.OpenWeb();
                currentEmp = properties.ListItem["Emp_Login"].ToString();
                    string myposition = properties.ListItem["Rank_Description"].ToString();
                    empManager = properties.ListItem["Manager_Name"].ToString();

                    SPSecurity.RunWithElevatedPrivileges(EnsurePermissionSet);
                    base.ItemAdded(properties);
               
                myWeb.Dispose();
            }

     private void EnsurePermissionSet()
            {
                SPList lst;
                SPListItem item;
                SPRoleAssignment roleAss;
                SPRoleDefinition roleDef;
                SPUser employee, manager;

                lst = myWeb.Lists["SharePointDocLib"];

                item.BreakRoleInheritance(true);
                myWeb.AllowUnsafeUpdates = true;

                manager= myWeb.EnsureUser(mgrFname);
                roleAss = new SPRoleAssignment(manager);
                roleDef = myWeb.RoleDefinitions["Contribute"];
                roleAss.RoleDefinitionBindings.Add(roleDef);
                item.RoleAssignments.Add(roleAss);


                employee= myWeb.EnsureUser(empName);
                roleAss = new SPRoleAssignment(employee);
                roleDef = myWeb.RoleDefinitions["Read"];
                roleAss.RoleDefinitionBindings.Add(roleDef);
                item.RoleAssignments.Add(roleAss);
                myWeb.AllowUnsafeUpdates = true;

            }

    Wednesday, November 20, 2013 1:50 AM

Answers

  • There are multiple issues. Let's start with this as your ItemAdded method:

            public override void ItemAdded(SPItemEventProperties properties)
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                    {
                        var web = properties.Web;
                        var item = properties.ListItem;
                        item.BreakRoleInheritance(true);
                        web.AllowUnsafeUpdates = true;
                        item.Update();
                    });
            }
    See if that works properly in your dev environment by deploying your solution, adding an item to the list and seeing if the new item has role inheritance broken.


    Doug Hemminger http://www.sharepointdoug.com

    Wednesday, November 20, 2013 3:07 AM

All replies

  • when not debugging, have you checked using Event Receiver Manager that the event is properly bound?

    have you included any logging (to the ULS) within your code?


    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com

    Wednesday, November 20, 2013 2:37 AM
  • Make sure your Event properly registered again the Item Upload.

    check this one, i believe this having same issue.

    http://social.msdn.microsoft.com/Forums/en-US/12bb095d-b54c-4749-960e-558ff73b61a1/event-receiver-does-not-fire-when-item-addedupdated-in-document-library


    Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem. ****************************************************************************************** Thanks -WS MCITP(SharePoint 2010, 2013) Blog: http://wscheema.com/blog

    Wednesday, November 20, 2013 2:50 AM
  • There are multiple issues. Let's start with this as your ItemAdded method:

            public override void ItemAdded(SPItemEventProperties properties)
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                    {
                        var web = properties.Web;
                        var item = properties.ListItem;
                        item.BreakRoleInheritance(true);
                        web.AllowUnsafeUpdates = true;
                        item.Update();
                    });
            }
    See if that works properly in your dev environment by deploying your solution, adding an item to the list and seeing if the new item has role inheritance broken.


    Doug Hemminger http://www.sharepointdoug.com

    Wednesday, November 20, 2013 3:07 AM
  • Thanks for all the responses. my code works fine during debugging. I am able to successfully complete the debugging and the permission assignment assigns correctly.

    The issue is only when I deploy the event receiver and when I upload the file it does not fire. I checked the same logic for list and it works fine.

    I then tried to add ListURL="Lists/DocumentLibrary" in element.xml file to target specific library and is of no use.

    Is there a way to capture the log for deployment process? I don't have any errors during debugging as it fires correctly.

    Wednesday, November 20, 2013 5:58 AM
  • One more update: The have created a timer job for the  same document library to update some of the user profile properties on a daily night. That works fine. Is that something causing problem to create event receiver not firing after deployment?

    Thanks

    Wednesday, November 20, 2013 6:52 AM
  • There are multiple issues with your code. For example, you should not declare your variables as class variables, which I am assuming you are doing since your declarations are missing from the code sample you posted. Second, you should not dispose of myWeb. You did not create myWeb, you simply assign it the value from the properties variable passed into the ItemAdded method. You are disposing of an SPWeb that should not be disposed. Third, you do not need to call the base ItemAdded method. 

    There are several additional issues in your EnsurePermissionsSet method. 

    I posted some code for you in my previous post. You need to start with that. Then we can add the rest of the stuff that you need.


    Doug Hemminger http://www.sharepointdoug.com

    Wednesday, November 20, 2013 2:48 PM
  • Thanks Doug. The code you provided works correctly after deploying it. It breaks the inheritance. What would you suggest? Would you suggest me to call the function inside the runwithelivated function?

    Thanks

    Wednesday, November 20, 2013 6:02 PM
  • If you can help me understand how you get the person's manager, I can help you with the rest of it. You need to get the SPUser object for the person's manager in order to assign permissions. You can't do that with ensureuser and the manager's first name (which is what I assume mgrFname is).

    Doug Hemminger http://www.sharepointdoug.com

    Wednesday, November 20, 2013 6:37 PM
  • I just updated/added additional lines to get empname and manager name from the item's metadata fields and then added them with contribute and read permission. The below code does not break the inheritance again(however Dougs code works all the time).

     public override void ItemAdded(SPItemEventProperties properties)
            {
              if (properties.ListTitle == "SPDocumentLib")
              {
                string currentEmp;
                string empManager;
            
                SPRoleAssignment roleAss;
                SPRoleDefinition roleDef;
                SPUser Advisee, Advisor;

                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    var web = properties.Web;
                    var item = properties.ListItem;
                    currentEmp = item["CR_Emp_Login"].ToString();
                    empManager = item["CR_Manager_Name"].ToString();

                    item.BreakRoleInheritance(true);
                    web.AllowUnsafeUpdates = true;

                    Advisor = web.EnsureUser(empManager);
                    roleAss = new SPRoleAssignment(Advisor);
                    roleDef = web.RoleDefinitions["Contribute"];
                    roleAss.RoleDefinitionBindings.Add(roleDef);
                    item.RoleAssignments.Add(roleAss);


                    Advisee = web.EnsureUser(currentEmp);
                    roleAss = new SPRoleAssignment(Advisee);
                    roleDef = web.RoleDefinitions["Read"];
                    roleAss.RoleDefinitionBindings.Add(roleDef);
                    item.RoleAssignments.Add(roleAss);
                    web.AllowUnsafeUpdates = true;

                    item.Update();
                });
              }
                base.ItemAdded(properties);
            }

    Any help?

    Wednesday, November 20, 2013 6:53 PM
  • I found out the root cause finally. Thanks to Doug for correcting the codeframework. 

    Doug, do you have any suggestion on the below details?

    It is  document library metadata issue and its not picking the metadata when the solution is deployed. When I am passing hardcode values for employee and manager name, the deployment works fine, breaking of inheritance and assigns permissions correctly

       currentEmp = item["CR_Emp_Login"].ToString();
         empManager = item["CR_Manager_Name"].ToString();

    Is it not possible to retrieve metadata of the uploaded file instantly in event handler? Is it require time to update(how long)? Please suggest that I need to retrive the uploaded file metadata to process the event handler functinilities.

     Thanks

    Wednesday, November 20, 2013 11:23 PM
  • Is this true? Cant we use metadata at all for document library? I see for ItemAdded it doesn't do anything ???? Any help or workaround?

    Event

    BeforeProperties

    AfterProperties

    ListItem

    ItemAdding

    Null

    Null

    Null

    ItemAdded

    Null

    Null

    New Value

    ItemUpdating

    Original Value

    Changed Value

    Original Value

    ItemUpdated

    Original Value

    Changed Value

    Changed Value

    ItemDeleting

    Null

    Null

    Original Value

    ItemDeleted

    Null

    Null

    Null

    Thursday, November 21, 2013 12:48 AM