2012-01-17 83 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

相關問題