关于代码调用SSP获取UserProfile出错的解决方案
2009-08-04 17:21
741 查看
用代码调用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
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
值不能为空。
参数名: 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();
}
代码如下:
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
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
值不能为空。
参数名: 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();
}
相关文章推荐
- 关于代码调用SSP获取UserProfile出错的解决方案(转)
- 学习:关于代码调用SSP获取UserProfile出错的解决方案(转)
- 关于代码调用SSP获取UserProfile出错的解决方案
- 关于android调用系统摄像头获取图片过小的解决方案
- 关于ServletRequest servletRequest中获取的流getInputStream()和.getReader()的方法只能调用一次的解决方法
- 关于用OC创建的项目调用swift代码
- IIS7.0出错的解决方案 IIS 状态代码:IIS详细错误代码以及解释
- 关于list.sort()中对象的属性无法调用compareTo()方法解决方案
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- java调用ffmpeg获取视频信息函数代码
- java代码运行中获取正被调用的函数名
- 关于ESP8266使用ArduinoIDE编程,调用airkiss_lan_recv和airkiss_lan_pack出错的解决方法
- 关于utc时间与格林威治时间(linux上获取系统时区及代码)
- 关于eclipse修改代码重启问题解决方案
- javascript如何调用C#后代码中的过程 和ASP.NET调用JS乱码解决方案
- win7注册ocx时出现对DllRegisterServer的调用失败,错误代码为0x80040200解决方案
- 关于ModalPopup控件不能调用CS事件代码的问题
- 高质量的C代码.关于获取随机数
- VS2010关于调用ffmpeg借口出错
- python 关于git下unix和windows换行符不同导致稍微改动一行代码git diff就是全部文件对比的解决方案