[英]Why It did not come into ItemAdded function When using RunWithElevatedPrivileges for limited access users in eventreceiver?
[英]why RunWithElevatedPrivileges does not work in itemAdded eventreceiver?
在我的eventreceiver項目itemAdded函數中,我的代碼將把項目添加到第二個列表中,但是它不適用於某些特權較低的用戶
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對象時,請確保使用提升的網站。 不要使用當前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 @ starting!”);)來記錄代碼中發生的事情,令人驚訝的是,我什至發現ItemAdded Function的第一行都沒有執行! 因為在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;
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.