2012-01-17 121 views
3

我的代码尝试在我的事件接收器中遍历SPListItems.RoleAssignments集合时收到错误。但只限于拥有贡献者权利的用户。拥有管理员权限的用户。使用下面的代码SharePoint 2010&“尝试执行未经授权的操作。” &SPListItems.RoleAssignments

  1. 结束语我下SPRunElevatedPrivlages
  2. 的Windows Impersation:

    我已经试过以下

    WindowsImpersonationContext ctx = null; 
    ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero); 
    SPUserToken oSysToken = GetSysToken(properties.SiteId) 
    private static SPUserToken GetSysToken(Guid SPSiteID) 
    { 
        SPUserToken sysToken = null; 
        using(SPSite oSite = new SPSite(SPSiteID)) 
        { 
         sysToken = oSite.SystemAccount.UserToken; 
        } 
        if (sysToken == null) 
        { 
         SPSecurity.RunWithElevatedPrivileges(
         delegate() 
         { 
          using(SPSite site = new SPSite(SPSiteID)) 
          { 
           sysToken = site.SystemAccount.UserToken; 
          } 
         }); 
        } 
        return sysToken; 
    } 
    
  3. 最后,我已经试过SPWeb.AllowUnsafeUpdates = true;

我已经尝试过所有的方法,并集体在一起,没有任何东西。它看起来像通过例外以及SPListItems.RoleAssignments.Count

回答

3

用户需要至少管理权限权限对象读取和修改SPSecurableObject.RoleAssignments

为了执行与系统帐户代码(高架),你必须“重新打开给定对象”一个SharePoint对象的权限:

SPListItem item = // the item from the event receiver 

// Re-open the item with the system account 
using (SPSite adminSite = new SPSite(item.Web.Url, SPUserToken.SystemAccount)) 
{ 
    using (SPWeb adminWeb = adminSite.OpenWeb()) 
    { 
    SPListItem adminItem = adminWeb 
     .Lists[item.ParentList.ID] 
     .GetItemByUniqueId(i.UniqueId); 

    // execute your code with the system account on the item. 
    // adminItem.RoleAssignments.WhatEver 
    } 
} 

请注意使用SPUserToken.SystemAccount。这使得SharePoint 2007中所需的“系统帐户令牌”破解已经过时。

对于SharePoint对象SPSecurity.RunWithElevatedPrivilegesWindowsIdentity.Impersonate(System.IntPtr.Zero)上的操作是而不是必需。

我写了一篇博客文章,涵盖了这个主题:How To Open a SPSite With thw System Account In SharePoint 2010

相关问题