zoukankan      html  css  js  c++  java
  • 关于代码调用SSP获取UserProfile出错的解决方案

    用代码调用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

    这个错误是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();
        }
  • 相关阅读:
    vue_源码 原理 剖析
    vue_vuex
    vue_VueRouter 路由_路由器管理n个路由_并向路由组件传递数据_新标签路由_编程式路由导航
    vue_mint-ui
    vue_ajax 请求
    vue_组件间通信:自定义事件、消息发布与订阅、槽
    vue_小项目_吃饭睡觉打豆豆
    vue-cli 脚手架 Command Line Interface
    程序员面试金典-面试题 04.03. 特定深度节点链表
    程序员面试金典-面试题 04.02. 最小高度树
  • 原文地址:https://www.cnblogs.com/jianyi0115/p/1148827.html
Copyright © 2011-2022 走看看