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

來源:互聯網
上載者:User
 

 

1、用代碼調用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;

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

 

源錯誤:

行46:   UserProfile u = _profileManager.GetUserProfile("saictest""zjy");

行47:

行48:        Response.Write(u.PersonalUrl);

 

這個錯誤是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;

 

        UserProfileManager _profileManager;       

       _profileManager = new UserProfileManager(context) ;

  

       UserProfile u = _profileManager.GetUserProfile("saictest""zjy");

 

       Response.Write(u.PersonalUrl);

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

 

注意:

1)網站應用程式集區的帳號必須有足夠的許可權,或者直接採用MOSS網站的應用程式集區.

2)網站的web.config下需要添加如下配置:

<identity impersonate="true" />

 

2、提升許可權後,操作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();

}

 

 

文章來源:http://www.cnblogs.com/jianyi0115/archive/2008/04/11/1148827.html

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.