SharePoint Web Service系列: Add或Update类型为User的项
2009-01-17 18:42
344 查看
SharePoint的任务列表中有一个字段叫做“分配对象”,就是为任务指派给某个用户。该字段的数据类型是User型的。在拼Web Service更新命令的串时,并不能像通常的字段一样直接给一个用户名做为值。
关于如何使用SharePoint提供的WebService进行列表的增删改,可以参考这里。
下面是该栏的相关信息:
有一点是肯定的,那就是一定是以字符串的方式来传值的。经过对列表项的架构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();
}
}
}
关于如何使用SharePoint提供的WebService进行列表的增删改,可以参考这里。
下面是该栏的相关信息:
内部名 | AssignedTo |
栏名 | 分配对象 |
类型 | User |
可筛选 | TRUE |
来源于基础类型 | FALSE |
隐藏 | FALSE |
只读 | FALSE |
可更改域的顺序 | TRUE |
必添字段 | FALSE |
可排序 | TRUE |
补充:我最近才发现,实际上这里只需要指定“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();
}
}
}
相关文章推荐
- SharePoint Web Service系列: Add或Update类型为User的项
- SharePoint Web Service系列: Add或Update类型为User的项
- SharePoint Web Service系列: Add或Update其他各种类型的项
- SharePoint Web Service系列: Add或Update其他各种类型的项
- SharePoint自动化系列——Create a local user and add to SharePoint
- SharePoint Web Service系列: Add或Update其他各种类型的项
- SharePoint Web Service系列:编写自定义SharePoint Web Services之一
- SharePoint 2010 UI 定制化系列之母板页重温:页面类型
- SharePoint 2013 图文开发系列之创建内容类型
- [导入]使用Sharepoint的Lists Web Service调用UpdateListItems方法"增/删/改"List Items
- SharePoint 2010 + Infopath 2010 VSTA + WF + Timer Job + BI 之 报销单实例系列------发布Infopath为内容类型并添加到库
- SharePoint自动化系列——Add/Remove “Hold” from items
- SharePoint 2010 UI 定制化系列之母板页重温:页面类型
- SharePoint Web Service系列:编写自定义SharePoint Web Services之二
- Sharepoint2010 系列:在Infopath 2010中调用Web Service(SharePoint Server 2010 -“User Profile Service We)
- Asp.net Identity 系列之 怎样修改Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的 Id 字段的数据类型
- SharePoint自带WebService(Lists Web Service--AddAttachment Method)
- SharePoint【调试,诊错系列】-- 在当前站点集或子网站中找到功能 {GUID} 中定义的 ID 为 0x0100GUID的内容类型
- SharePoint 2007 开发系列(22) SharePoint内容类型Content Types
- SharePoint 2007 系列(3) -Add top link bar