缘来路过 发表于 2015-9-27 08:22:47

SharePoint Web Service系列: Add或Update类型为User的项

SharePoint的任务列表中有一个字段叫做“分配对象”,就是为任务指派给某个用户。该字段的数据类型是User型的。在拼Web Service更新命令的串时,并不能像通常的字段一样直接给一个用户名做为值。  关于如何使用SharePoint提供的WebService进行列表的增删改,可以参考这里。
  下面是该栏的相关信息:
  内部名
  AssignedTo
  栏名
  分配对象
  类型
  User
  可筛选
  TRUE
  来源于基础类型
  FALSE
  隐藏
  FALSE
  只读
  FALSE
  可更改域的顺序
  TRUE
  必添字段
  FALSE
  可排序
  TRUE
  有一点是肯定的,那就是一定是以字符串的方式来传值的。经过对列表项的架构xml的分析,发现了这个字符串的格式为 “UserID;#UserName”。

补充:我最近才发现,实际上这里只需要指定“UserID”就可以了。而且在做Cmd="New"操作时,必须是使用UserID的。在新增时写成上面的格式会返回错误。^_^

  那么,只需要在调用UpdateListItem之前调用另一个获取用户信息的WebService先得到这些信息就可以顺利实现对包含该类型字段的列表项进行更新了。
  下面是示例的代码,在vs2005中调试通过。其中引用了两个SharePoint的WebService.。分别是
  Lists Service
  Web引用Url:http://Server_Name/[Site_Name/]_vti_bin/Lists.asmx
  文件夹名称:LabDb
  Users and Groups Service
  Web引用Url:http://Server_Name/[Site_Name/]_vti_bin/UserGroup.asmx
  文件夹名称:LabUser


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ConsoleTestUpdate
{
    class Program
    {
      static void Main(string[] args)
      {
            LabDb.Lists listService = new LabDb.Lists();
            LabUser.UserGroup userService = new LabUser.UserGroup();
            userService.Credentials = listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            string UserID = "";
            string UserName = "";
            try
            {
                XmlNode ndUserInfo = userService.GetUserInfo("lab\\sunmoonfire");
                UserID = ndUserInfo.ChildNodes.Attributes["ID"].Value.ToString();
                UserName= ndUserInfo.ChildNodes.Attributes["Name"].Value.ToString();
               
            }
            catch { }
            if ((UserID != null && UserID != "") && (UserName != "" && UserName != null))
            {
                string strBatch = "<Method ID='1' Cmd='Update'>" +
                               "<Field Name='ID'>1</Field>" +
                               "<Field Name='AssignedTo'>" +
                               UserID + ";#" + UserName + "</Field></Method>";

                XmlDocument xmlDoc = new System.Xml.XmlDocument();

                System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
                elBatch.SetAttribute("OnError", "Continue");
                elBatch.InnerXml = strBatch;
                try
                {
                  XmlNode ndReturn = listService.UpdateListItems("任务", elBatch);
                  //XmlNode ndReturn = listService.GetListItems("任务",null,null,null,null,null);      //查看返回的列表项的结构,用于分析串的组成
                  Console.WriteLine(ndReturn.OuterXml);
      
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("bad parameter");
            }
            Console.Read();
      }
    }
}
页: [1]
查看完整版本: SharePoint Web Service系列: Add或Update类型为User的项