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