给InfoPath添加VSTA托管代码并从SharePoint列表查询数据

在Loading事件中参加如下的代码:
      if (this.New) //假如是新的表单,就配置原始值 
      { 
        //取得当前用户在配置文件中FullName的值 
        using (SPSite site = new SPSite("http://oss")) //请把这个值换成你的SharePoint站点的URL 
        { 
          //以下根据当前用户登录名来取得用户的配置文件 
          ServerContext context = ServerContext.GetContext(site); 
          UserProfileManager profileManager = new UserProfileManager(context);       
          UserProfile u = profileManager.GetUserProfile(this.Application.User.LoginName); 
          string fullName = u[PropertyConstants.PreferredName].Value.ToString(); 
 
          windows xp athNavigator mainNavigator = this.MainDataSource.CreateNavigator();  //取得主数据源的检索游标 
 
          //配置申请人的节点值为用户名,即配置文件中PreferredName(名称)的属性          
          mainNavigator.SelectSingleNode("/my:myFields/my:申请人", this.NamespaceManager).SetValue(fullName);  
          
          //根据姓名从SharePoint的部门列表来查询部门 
          using (SPWeb web = site.AllWebs["/demo"]) //请把这个换成你的SharePoint站点的URL 
          { 
            SPQuery query = new SPQuery(); 
            //因为姓名字段,是从标题修改而来的,所以其内部名称为Title 
            query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + fullName + "</Value></Eq></Where>"; 
            SPList list = web.Lists["部门人员"]; 
            SPListItemCollection items = list.GetItems(query); 
            if (items.Count > 0) //假如查找到了就配置部门的值 
              mainNavigator.SelectSingleNode("/my:myFields/my:申请部门", this.NamespaceManager).SetValue(items[0]["部门"].ToString ());  
 
          } 
 
        } 
      } 
    }
原文地址:https://www.cnblogs.com/IsNull/p/1800925.html