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

it2022-05-05  55

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/[sites/][Site_Name/]_vti_bin/Lists.asmx

文件夹名称:LabDb

Users and Groups Service

Web引用Url:http://Server_Name/[sites/][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[ 0 ].Attributes[ " ID " ].Value.ToString();                UserName =  ndUserInfo.ChildNodes[ 0 ].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();        }    }}

转载于:https://www.cnblogs.com/Sunmoonfire/archive/2006/10/25/539815.html

相关资源:数据结构—成绩单生成器

最新回复(0)