C# 为什么RunWithElevatedPrivileges在itemAdded eventreceiver中不起作用?
在我的eventreceiver项目itemAdded函数中,我的代码将把项添加到第二个列表中,但它不适用于某些权限较低的用户C# 为什么RunWithElevatedPrivileges在itemAdded eventreceiver中不起作用?,c#,sharepoint,event-receiver,C#,Sharepoint,Event Receiver,在我的eventreceiver项目itemAdded函数中,我的代码将把项添加到第二个列表中,但它不适用于某些权限较低的用户 SPSecurity.RunWithElevatedPrivileges(delegate () { using (SPSite site = new SPSite(properties.SiteId)) { using (SPWeb web
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.Web.ID))
{
web.AllowUnsafeUpdates = true;
//my code
web.AllowUnsafeUpdates = false;
}
}
}
获取SPList对象时,请确保使用提升的web。不使用当前SPContext或事件接收器属性中的SPWeb 因此,在您的情况下,获取列表应该如下所示:
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.Web.ID))
{
web.AllowUnsafeUpdates = true;
SPList someList = web.Lists.tryGetList("LISTNAME");
SPListItem newItem = someList.AddItem();
// .... update columns and newItem.Update()
web.AllowUnsafeUpdates = false;
}
}
}
如果这不起作用,请提供更多的代码来检查,可能还有存在的错误 我写了一些代码(LogInfo(“event@receiver@开始!);)来记录代码中发生的事情,我惊奇地发现,即使是ItemAdded函数的第一行也没有执行!!!
因为在shapreoint日志中找不到任何内容。这意味着它没有输入ItemAdded函数或其他内容。
这是我的密码:
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;
}
}
});
}
谢谢@Adam,问题在于用户的权限级别。它只是在视图级别