0

在Web部件中,沒有任何權限的用戶需要讀取和更新Sharepoint列表。使用提升的權限更新Sharepoint列表

提升權限適用於閱讀列表,但是當我嘗試更新同一列表時,將引發異常。如何以更高的權限更新列表?

SPSecurity.RunWithElevatedPrivileges(delegate() 
{ 

SPSite oSite = SPControl.GetContextSite(HttpContext.Current); 
SPWeb oWeb = oSite.OpenWeb(); 
oWeb.AllowUnsafeUpdates = true; 
SPListItemCollection listItems = oWeb.Lists["nameList"].Items; 
SPListItem item = listItems.Add(); 

... 

item.Update(); // Throws Exception 
}); 
+0

異常發送一個「不能達到引起斷章取義跟蹤」的消息。 – 2011-05-18 12:20:31

+0

我建議使用在正確的代碼中使用,你不應該放棄spsite和spweb對象。如果你沒有直接從spcontext獲得它,或者你的代碼寫在正確的代碼上面 – 2011-05-19 05:49:20

+0

基本上是一個副本。看看這個答案:http://stackoverflow.com/questions/6043312/sharepoint-query-with-elevated-privileges – trgraglia 2011-05-18 13:32:30

回答

0

問題是SPSite的創建。

正確的代碼:

SPSite oSite = new SPSite(SPContext.Current.Site.ID); 
SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID);         
listItems = oWeb.Lists["nameList"]; 
1

升高的privilages用於添加編輯和刪除所有操作,以便thier必須有一些其他的問題請提供異常的詳細信息

+0

是的,是真的。這是由SPSite的創建造成的。 – 2011-05-18 12:21:01

+0

在這種情況下,您必須創建spsite和spweb的新實例,並且您不必從SPControl.GetContextSite(HttpContext.Current)中消費一個站點對象 – 2011-05-19 05:47:29