为什么在事件接收器中为受限访问用户使用RunWithElevatedPrivileges时,为什么它没有加入ItemAdded函数?

时间:2019-07-05 20:49:13

标签: c# sharepoint event-receiver

II具有一个列表的事件接收器,并在ItemAdded函数的另一个列表中进行一些更新。由于,它不适用于访问受限的用户,因此我尝试使用 SPSecurity.RunWithElevatedPrivileges用于授予此类用户访问权限。但是,它仍然不适用于访问受限的用户。我尝试记录函数中发生的情况。令人震惊的是,即使在第一行中,我的ItemAdded函数也无法正常工作(LoLogInfo(“ event @ receiver @ starting!”);)。我确定是因为我检查了共享点日志文件。我怎么了?这是我的代码:

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 个答案:

答案 0 :(得分:0)

代码很好。如果事件接收器未触发,则意味着它未附加到列表中。

请检查是否:

  1. 请添加


base.ItemAdded(properties);

最佳做法是在覆盖功能的开头(在您的自定义代码之前)

    事件接收器中的
  1. 您应该具有elements.xml,请检查是否设置了ItemAdded事件。应该有一个类型为ItemAdded的接收者节点 enter image description here

  2. 请检查事件接收器是否包含在功能组件中(有时不包含事件接收器,也未随功能一起部署)。该功能应该是范围网站或网站

  3. 部署项目(尝试收回项目并从头开始部署)

  4. 部署后,请检查该功能是否已激活。

请注意,将新事件添加到事件接收器时,您需要始终重新部署项目并响应该功能。