2011-03-14 117 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