2012-05-23 50 views
1

激活站點功能時,我自動想要設置WebApplication屬性。這是代碼:沒有權限訪問web應用程序屬性

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
     {    
       SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties); 
       using (SPSite site = new SPSite(currentWeb.Site.Url)) 
       { 
        SPWebApplication currentApplication = site.WebApplication; 
        if (currentApplication.MaxQueryLookupFields < 20) 
        { 

         SPSecurity.RunWithElevatedPrivileges(delegate() 
         { 
          try 
          { 
           currentApplication.MaxQueryLookupFields = 20; 
          } 

          catch (System.Security.SecurityException ex) 
          { 
           _log.ErrorFormat("no permission"); 
          } 
         }); 

        } 
       }     
     } 

即使我場管理員激活了該功能,安全異常也會被拋出(「拒絕訪問」)。在行

currentApplication.MaxQueryLookupFields = 20; 

AFAIK SPSecurity.RunWithElevatedPrivileges運行的網站管理員,農場管理。但如何做到這一點? (不RunWithElevatedPrivileges我得到相同的異常。

回答

0

您需要創建新的SPSite的SPSecurity.RunWithElevatedPrivileges內的SPWeb和SPWebApplication對象,否則你將有相同的權限當前用戶運行它們。例如

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
     { 
     SPSecurity.RunWithElevatedPrivileges(delegate() 
      {    
       SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties); 
       using (SPSite site = new SPSite(currentWeb.Site.Url)) 
       { 
        SPWebApplication currentApplication = site.WebApplication; 
        if (currentApplication.MaxQueryLookupFields < 20) 
        { 
          try 
          { 
           currentApplication.MaxQueryLookupFields = 20; 
          } 

          catch (System.Security.SecurityException ex) 
          { 
           _log.ErrorFormat("no permission"); 
          } 


        } 
       } 
      });     
     } 
+0

我嘗試**沒有** RunWithElevatedPrivilegues並以農場管理員身份登錄。仍然有錯誤。 問題是,RunWith ..作爲網站管理員執行,而不是農場管理員。因此,我搜索「像」RunWith ......但可以與農場管理員權限一起使用 –

0

您應該實例裏面RWEP另一個SPSite對象獲取應用程序池的身份背景,因爲外面的RWEP塊與當前的SPSite用戶上下文創建其中創建了第一的SPSite那麼試試這個:

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
    { 
     SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties); 
     using (SPSite site = new SPSite(currentWeb.Site.Url)) 
     { 
      SPSecurity.RunWithElevatedPrivileges(delegate() 
      { 
       using (SPSite _site = new SPSite(site.ID)) 
       { 
        SPWebApplication currentApplication = _site.WebApplication; 
        if (currentApplication.MaxQueryLookupFields < 20) 
        { 
         try 
         { 
          currentApplication.MaxQueryLookupFields = 20; 
         } 
         catch (System.Security.SecurityException ex) 
         { 
          _log.ErrorFormat("no permission"); 
         } 
        } 
       } 
      }); 
     } 
    } 
相關問題