關於代碼調用SSP擷取UserProfile出錯的解決方案)

來源:互聯網
上載者:User

轉(http://www.cnblogs.com/jianyi0115/archive/2008/04/11/1148827.html)

用代碼調用MOSS的SSP擷取UserProfile時,代碼只能運行於MOSS網站下,或者是WinForm中,否則,會出錯,這個問題如何解決呢?

代碼如下:

SPSite site = new SPSite("http://ssjin073:9031");        

        //擷取上下文環境
        ServerContext context = ServerContext.GetContext( site ) ;//.GetContext(sspName);

       //this.Context.Items["Microsoft.Office.ServerContext"] =  context;
       
        Response.Write(context.Status);

        UserProfileManager _profileManager;        
       _profileManager = new UserProfileManager(context) ;
   
       UserProfile u = _profileManager.GetUserProfile("saictest\\zjy");

       Response.Write(u.PersonalUrl);

這段代碼只能在MOSS網站的頁面上運行,如果在普通的網站或者直接在VS的小IIS中運行,會報如下錯誤:

Error
值不可為空。
參數名: serverContext 
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆疊追蹤資訊,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細資料。 

異常詳細資料: System.ArgumentNullException: 值不可為空。
參數名: serverContext

源錯誤: 


行 44: 
行 45:    
行 46:        UserProfile u = _profileManager.GetUserProfile("saictest\\zjy");
行 47: 
行 48:        Response.Write(u.PersonalUrl);
 

源檔案: e:\DEV\zjy\MCS Enterprise Solution\MCS Enterprise Solution\SharePoint\MCS.SharePoint.Web\Default.aspx.cs    行: 46 

堆疊追蹤: 


[ArgumentNullException: 值不可為空。
參數名: serverContext]
   Microsoft.Office.Server.SiteContext..ctor(ServerContext serverContext) +79
   Microsoft.Office.Server.SiteContext.get_Current() +128
   Microsoft.Office.Server.UserProfiles.SRPSite.get_SiteContext() +36
   Microsoft.Office.Server.UserProfiles.PropertyDataTypeCollection..ctor(SRPSite site) +264
   Microsoft.Office.Server.UserProfiles.SRPSite.get_DataTypes() +48
   Microsoft.Office.Server.UserProfiles.UserProfileManager.get_PropertyDataTypes() +58
   Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReader myReader, Boolean bFirstRead, Boolean bWssId) +116
   Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReader myReader) +33
   Microsoft.Office.Server.UserProfiles.UserProfile.RetrieveUser(String strAcct, Guid gAcct, Byte[] bSid, Nullable`1 recordId, Boolean doNotResolveToMasterAccount) +703
   Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct, Boolean doNotResolveToMasterAccount, Boolean forceUserIsSelf) +329
   Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct) +37
   Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName) +129
   _Default.Page_Load(Object sender, EventArgs e) in e:\DEV\zjy\MCS Enterprise Solution\MCS Enterprise Solution\SharePoint\MCS.SharePoint.Web\Default.aspx.cs:46
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
   System.Web.UI.Control.OnLoad(EventArgs e) +80
   System.Web.UI.Control.LoadRecursive() +49
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3743

 

這個錯誤是MOSS的一個bug引起的,SiteContext的內部代碼會調用當前上下文中的ServerContext,調用不到,就抱錯了。

我們可以通過手工添加內容物件解決這個問題 :

 SPSite site = new SPSite("http://ssjin073:9031");        

        //擷取上下文環境
        ServerContext context = ServerContext.GetContext( site ) ;//.GetContext(sspName);

        //此處將ServerContext放入上下文
       this.Context.Items["Microsoft.Office.ServerContext"] =  context;
       
        Response.Write(context.Status);

        UserProfileManager _profileManager;        
       _profileManager = new UserProfileManager(context) ;
   
       UserProfile u = _profileManager.GetUserProfile("saictest\\zjy");

       Response.Write(u.PersonalUrl);

以上代碼在任何網站下都可以運行了!

注意:
1)網站應用程式集區的帳號必須有足夠的許可權,或者直接採用MOSS網站的應用程式集區.
2)網站的web.config下需要添加如下配置:

<identity impersonate="true" />

----------
另:提升許可權後,操作ListItem也會出現類似的錯誤,可以用同樣的方法解決:

protected void Page_Load(object sender, EventArgs e)   
    {

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            UpdateItem();
        }
        );           
    }

    void UpdateItem()
    {
        SPSite site = new SPSite("http://ssjin073:9032");
        
        SPWeb web = site.RootWeb;
          
        //強制設定內容物件
        HttpContext.Current.Items["HttpHandlerSPWeb"] = web;

        web.AllowUnsafeUpdates = true;
        SPList list = web.Lists["MainList"];

        SPListItem item = list.Items[0];

        item["Title"] = DateTime.Now.ToString();

        item.Update();

        web.Dispose();
        site.Dispose();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.