因工作需要对一个SharePoint的子站点进行更新,以前是手工做的。由于我不是这个站点的管理员,因此GOOLE之后,找到了这个方案,作了稍许修改。体会是:
1. 如果要更新的站点是一个子站点,即使已经添加了Web Service引用,还要在Uri属性中再次声明引用列表路径,否则会报错0x81020026;
2. 如果有日期字段,在XML文件中要设成yyyy-MM-ddTHH:mm:ssZ格式,否则会报日期格式不正确的错误;
3. XML文件中指定的字段名如有空格,要用_x0020_连接。如"User Name"要写成"User_x0020_Name"。
以下代码在VS2008中调试通过。
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace WebServiceTest1
{
class Program
{
static void Main(string[] args)
{
try
{
test();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("END");
Console.Read();
}
private static void test()
{
LabPortal.Lists listService = new LabPortal.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = "http://site_name/subsite/_vti_bin/Lists.asmx"; //!!!
//ID是Method的唯一标识,如果有多个Method就都列在下面
//Cmd的类型有:New,Update,Delete。分别对应增加,删除,更新
"<Field Name='Date'>2008-09-12T09:00:00Z</Field>" + // use yyyy-MM-ddTHH:mm:ssZ format
"<Field Name='User_0x0020_name'>Ally McBeal</Field>" +
"</Method>";
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
//Batch元素下面的这些Attribue是可选的
elBatch.SetAttribute("OnError", "Continue"); //指定出错后是返回还是继续下一步
elBatch.SetAttribute("ListVersion", "1"); //指定列表的版本
//elBatch.SetAttribute("ViewName", "654446D3-8E70-4483-B2B6-F87329EAC2D9"); //指定所操作的列表视图GUID
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems("Test", elBatch); //在名为Test的列表中增加一条记录
Console.WriteLine(ndReturn.OuterXml); }
}
}