0

II have an event receiver of a list and do some updates in another list in ItemAdded function. since, It does not work for limited access users, so I try to use SPSecurity.RunWithElevatedPrivileges for giving access for such users. But It still does not work for limited access users. I try to log what is going on in my function. shockingly, my ItemAdded function did not work even in the first line(LoLogInfo("event@receiver@ starting!");). I make sure of it because I checked sharepoint log file.What is wrong with me? here is my code:

public override void ItemAdded(SPItemEventProperties properties)
{
    LoLogInfo("event@receiver@ starting!");
    SPSecurity.RunWithElevatedPrivileges(delegate ()
    {
        LogInfo("event@receiver@ first step!");
        using (SPSite site = new SPSite(properties.SiteId))
        {
            LogInfo("event@receiver@ second step!");
            using (SPWeb web = site.OpenWeb(properties.Web.ID))
            {
                LogInfo("event@receiver@ third step!");
                SPList activeList = web.Lists.TryGetList(properties.List.Title);
                SPList finalList = web.Lists[FinalListName];
                web.AllowUnsafeUpdates = true;
                SPListItem finalListItem = finalList.AddItem();
                LogInfo("event@receiver@ forth step!");
                //some other code here
                web.AllowUnsafeUpdates = false;
                }
         }                
    });
}

1 Answers1

0

the code is fine. If the event receiver does not trigger then this means it is not attached to the list.

Please check if:

  1. please include the


base.ItemAdded(properties);

at the beginning (before Your custom code) of the override function as best practice.

  1. in the event receiver You should have the elements.xml, please check if the ItemAdded event is set. There Should a receiver node with type ItemAdded enter image description here

  2. please check if the event receiver is included to a feature components (sometimes they are not included and are not deployed with the feature). The feature should be scope web or site

  3. deploy the project (try maybe retract the project and run deploy from scratch)

  4. after deploy please check if the feature is activated.

Please be aware that when adding new event to the event receiver You need to always redeploy the project and reactive the feature.

Adam
  • 810
  • 7
  • 10