2011-03-14 94 views
3

我已經在SharePoint 2007中創建了測驗Web部件,但卡住了一個權限。它需要將測驗分數寫入一個列表,在嘗試時現在會拋出一個錯誤。我假設如果Web部件具有適當的權限級別,則用戶的權限(測驗者)無關緊要。SharePoint Web部件列表寫入權限

是否有一個特定的權限應該允許webpart寫入列表?具體來說:

SPListItem item = listItems.Add(); 

回答

2

如果當前用戶對列表進行寫入(貢獻)訪問,則webpart只能寫入列表。如果您不希望用戶有權訪問該列表,則可以提升寫入列表的權限。這將導致列表項目由系統帳戶創建。

SPSecurity.RunWithElevatedPrivileges(WriteToList); 

private void WriteToList() 
{ 
    // create new SPSite and SPWeb objects. This is important, if you 
    // don't then the write won't use the elevated privileges. 
    using(SPSite site = new SPSite(SPContext.Current.Site.ID)) 
    { 
     using(SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) 
     { 
      // Code to Write to the list. 
     } 
    } 
} 

欲瞭解更多信息,請參閱http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

+0

+1,但我更喜歡具有對SPSite的'using'塊,以及...在我看來,更清晰的大括號。 – 2011-03-15 02:01:54

+1

@你的願望是我的命令。 :P – 2011-03-15 02:32:17

+0

感謝凱爾幫助我的大括號OCD。 ; d – 2011-03-15 13:13:41