关于代码调用SSP获取UserProfile出错的解决方案(转)


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

用代码调用MOSS的SSP获取UserProfile时,代码只能运行于MOSS站点下,或者是WinForm中,否则,会出错,这个问题如何解决呢?

代码如下:

SPSitesite=newSPSite("http://ssjin073:9031");

//获取上下文环境
ServerContextcontext=ServerContext.GetContext(site);//.GetContext(sspName);

//this.Context.Items["Microsoft.Office.ServerContext"]=context;

Response.Write(context.Status);

UserProfileManager_profileManager;
_profileManager
=newUserProfileManager(context);

UserProfileu
=_profileManager.GetUserProfile("saictest\\zjy");

Response.Write(u.PersonalUrl);


这段代码只能在MOSS站点的页面上运行,如果在普通的站点或者直接在VS的小IIS中运行,会报如下错误:


值不能为空。
参数名:serverContext
说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息:System.ArgumentNullException:值不能为空。
参数名:serverContext

源错误:


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


源文件:e:\DEV\zjy\MCSEnterpriseSolution\MCSEnterpriseSolution\SharePoint\MCS.SharePoint.Web\Default.aspx.cs行:46

堆栈跟踪:


[ArgumentNullException:值不能为空。
参数名:serverContext]
Microsoft.Office.Server.SiteContext..ctor(ServerContextserverContext)+79
Microsoft.Office.Server.SiteContext.get_Current()+128
Microsoft.Office.Server.UserProfiles.SRPSite.get_SiteContext()+36
Microsoft.Office.Server.UserProfiles.PropertyDataTypeCollection..ctor(SRPSitesite)+264
Microsoft.Office.Server.UserProfiles.SRPSite.get_DataTypes()+48
Microsoft.Office.Server.UserProfiles.UserProfileManager.get_PropertyDataTypes()+58
Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReadermyReader,BooleanbFirstRead,BooleanbWssId)+116
Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReadermyReader)+33
Microsoft.Office.Server.UserProfiles.UserProfile.RetrieveUser(StringstrAcct,GuidgAcct,Byte[]bSid,Nullable`1recordId,BooleandoNotResolveToMasterAccount)+703
Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManagerobjManager,StringstrAcct,BooleandoNotResolveToMasterAccount,BooleanforceUserIsSelf)+329
Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManagerobjManager,StringstrAcct)+37
Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(StringstrAccountName)+129
_Default.Page_Load(Objectsender,EventArgse)ine:\DEV\zjy\MCSEnterpriseSolution\MCSEnterpriseSolution\SharePoint\MCS.SharePoint.Web\Default.aspx.cs:46
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtrfp,Objecto,Objectt,EventArgse)+13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Objectsender,EventArgse)+45
System.Web.UI.Control.OnLoad(EventArgse)+80
System.Web.UI.Control.LoadRecursive()+49
System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+3743



这个错误是MOSS的一个bug引起的,SiteContext的内部代码会调用当前上下文中的ServerContext,调用不到,就抱错了。

我们可以通过手工添加上下文对象解决这个问题 :

SPSitesite=newSPSite("http://ssjin073:9031");

//获取上下文环境
ServerContextcontext=ServerContext.GetContext(site);//.GetContext(sspName);

//此处将ServerContext放入上下文
this.Context.Items["Microsoft.Office.ServerContext"]=context;

Response.Write(context.Status);

UserProfileManager_profileManager;
_profileManager=newUserProfileManager(context);

UserProfileu=_profileManager.GetUserProfile("saictest\\zjy");

Response.Write(u.PersonalUrl);


以上代码在任何站点下都可以运行了!

注意:
1)站点应用程序池的帐号必须有足够的权限,或者直接采用MOSS站点的应用程序池.
2)站点的web.config下需要添加如下配置:

<identityimpersonate="true"/>


----------
另:提升权限后,操作ListItem也会出现类似的错误,可以用同样的方法解决:

protectedvoidPage_Load(objectsender,EventArgse)
{

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

);
}


voidUpdateItem()
{
SPSitesite
=newSPSite("http://ssjin073:9032");

SPWebweb
=site.RootWeb;

//强制设置上下文对象
HttpContext.Current.Items["HttpHandlerSPWeb"]=web;

web.AllowUnsafeUpdates
=true;
SPListlist
=web.Lists["MainList"];

SPListItemitem
=list.Items[0];

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

item.Update();

web.Dispose();
site.Dispose();
}
优质内容筛选与推荐>>
1、解决存取Cookie时遇到编码出错的问题
2、spring依赖注入原理
3、解决Axis2在webservice中遇到特殊字符的无法传输的缺陷
4、评论列表显示及排序,个人中心显示
5、Asp.Net MVC中模型、视图和控制器简述


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn