Fork me on GitHub
sharepoint Lists Web service 用法

概述

在sharepoint 项目中,后期做数据迁移时,会用到sharepoint的web service来完成把数据导入sharepoint站点的功能。

web service 名称:

http://[site]/_vti_bin/Lists.asmx

我们用它来新增,修改或者删除当前站点特定list 的item操作。

调用的方法:

1 [SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/UpdateListItems", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
2 public XmlNode UpdateListItems (
3     string listName,
4     XmlNode updates
5 )

listName:当前站点list的名字。

操作list的item则通过XmlNode来完成。

更改普通list里item对应field Name的xml代码:

01 <Batch OnError="Continue" ListVersion="1" 
02 ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
03    <Method ID="1" Cmd="Update">
04       <Field Name="ID">4<Field>
05       <Field Name="Field_Name">Value</Field>
06    </Method>
07    <Method ID="2" Cmd="Update">
08       <Field Name="ID" >6</Field>
09       <Field Name="Field_Name">Value</Field>
10    </Method>
11 </Batch>

新增list里item的xml代码:

01 <Batch OnError="Continue" ListVersion="1" 
02 ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
03    <Method ID="1" Cmd="New">
04       <Field Name='ID'>New</Field>
05       <Field Name="Title">Value</Field>
06       <Field Name="Date_Column">2007-3-25</Field>
07       <Field Name="Date_Time_Column">
08          2006-1-11T09:15:30Z</Field>
09    </Method>
10 </Batch>

删除list里item的xml代码:

1 <Batch OnError="Continue" ListVersion="1" 
2 ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
3    <Method ID="1" Cmd="Delete">
4       <Field Name='ID'>2</Field>
5    </Method>
6    <Method ID="2" Cmd="Delete">
7       <Field Name='ID'>8</Field>
8    </Method>
9 </Batch>

document libraries操作:

新建文件夹xml的代码:

1 <Batch OnError="Continue" PreCalc="TRUE" 
2 ListVersion="0" 
3 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
4    <Method ID="1" Cmd="New">
5       <Field Name="ID">New</Field>
6       <Field Name="FSObjType">1</Field>
7       <Field Name="BaseName">Name</Field>
8    </Method>
9 </Batch>

更新文件夹的xml代码:

01 <Batch OnError="Continue" PreCalc="TRUE" 
02 ListVersion="0" 
03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
04    <Method ID="1" Cmd="Update">
05       <Field Name="ID">3</Field>
06       <Field Name="owshiddenversion">1</Field>
07       <Field Name="FileRef">
08          http://Server/[sites/][Site/]Shared 
09          Documents/Folder</Field>
10       <Field Name="FSObjType">1</Field>
11       <Field Name="BaseName">Name</Field>
12    </Method>
13 </Batch>

删除文件夹xml代码:

01 <Batch OnError="Continue" PreCalc="TRUE" 
02 ListVersion="0" 
03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
04    <Method ID="1" Cmd="Delete">
05       <Field Name="ID">4</Field>
06       <Field Name="FileRef">
07          http://Server/[sites/][Site/]Shared 
08          Documents/Folder</Field>
09    </Method>
10 </Batch>

更新文件夹里文件的xml代码:

01 <Batch OnError="Continue" PreCalc="TRUE" 
02 ListVersion="0" 
03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
04    <Method ID="1" Cmd="Update">
05       <Field Name="ID">2</Field>
06       <Field Name="owshiddenversion">1</Field>
07       <Field Name="FileRef">
08          http://Server/[sites/][Site/]Shared 
09          Documents/File</Field>
10       <Field Name="BaseName">Name</Field>
11    </Method>
12 </Batch>

删除文件夹里文件的xml代码:

01 <Batch OnError="Continue" PreCalc="TRUE" 
02 ListVersion="0" 
03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
04    <Method ID="1" Cmd="Delete">
05       <Field Name="ID">3</Field>
06       <Field Name="FileRef">
07          http://Server/[sites/][Site/]Shared 
08          Documents/File</Field>
09    </Method>
10 </Batch>

调用方法:

01 Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
02 listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
03   
04 string strBatch = "<Method ID='1' Cmd='Update'>"
05     "<Field Name='ID'>4</Field>" +
06     "<Field Name='Field_Number'>999</Field></Method>" +
07     "<Method ID='2' Cmd='Update'><Field Name='ID' >6</Field>" +
08     "<Field Name='Field_DateTime'>
09         2003-11-11T09:15:30Z</Field></Method>"; 
10   
11 XmlDocument xmlDoc = new System.Xml.XmlDocument();
12   
13 System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
14   
15 elBatch.SetAttribute("OnError","Continue");
16 elBatch.SetAttribute("ListVersion","1");
17 elBatch.SetAttribute("ViewName",
18     "0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40");
19   
20 elBatch.InnerXml = strBatch;
21   
22 XmlNode ndReturn = listService.UpdateListItems("List_Name", elBatch);
23   
24 MessageBox.Show(ndReturn.OuterXml);

返回XmlNode代码的格式:

02    <Result ID="1,Update">
03       <ErrorCode>0x00000000</ErrorCode>
04       <z:row ows_ID="4" ows_Title="Title" 
05          ows_Modified="2003-06-19 20:31:21" 
06          ows_Created="2003-06-18 10:15:58" 
07          ows_Author="3;#User1_Display_Name" 
08          ows_Editor="7;#User2_Display_Name" ows_owshiddenversion="3" 
09          ows_Attachments="-1" 
10          ows__ModerationStatus="0" ows_LinkTitleNoMenu="Title" 
11          ows_LinkTitle="Title" 
12          ows_SelectTitle="4" ows_Order="400.000000000000" 
13          ows_GUID="{4962F024-BBA5-4A0B-9EC1-641B731ABFED}" 
14          ows_DateColumn="2003-09-04 00:00:00" 
15          ows_NumberColumn="791.00000000000000" 
16          xmlns:z="#RowsetSchema" />
17    </Result>
18    <Result ID="2,Update">
19       <ErrorCode>0x00000000</ErrorCode>
20       <z:row ows_ID="6" ows_Title="Title" 
21          ows_Modified="2003-06-19 20:31:22" 
22          ows_Created="2003-06-18 19:07:14" 
23          ows_Author="2;#User1_Display_Name" 
24          ows_Editor="6;#User2_Display_Name" ows_owshiddenversion="4" 
25          ows_Attachments="0" ows__ModerationStatus="0" 
26          ows_LinkTitleNoMenu="Title" 
27          ows_LinkTitle="Title" ows_SelectTitle="6" 
28          ows_Order="600.000000000000" 
29          ows_GUID="{2E8D2505-98FD-4E3E-BFDA-0C3DEBE483F7}" 
30          ows_DateColumn="2003-06-23 00:00:00" 
31          ows_NumberColumn="9001.00000000000000" 
32          xmlns:z="#RowsetSchema" />
33    </Result>
34    ...
35 </Results>

取返回值的方法:

01 foreach (XmlNode node in nodes)
02 {
03     if (node.Name == "rs:data")
04     {
05         for (int i = 0; i < node.ChildNodes.Count; i++)
06         {
07             if (node.ChildNodes[i].Name == "z:row")
08             {
09                   
10                 string ID = node.ChildNodes[i].Attributes["ows_ID"].Value;
11                 string Title = node.ChildNodes[i].Attributes["ows_Title"].Value;
12  
13             }
14         }
15     }
16 }

总结

简单介绍了Lists.UpdateListItems (string listName,XmlNode node)的用法。

posted on 2011-07-19 23:10  HackerVirus  阅读(274)  评论(0编辑  收藏  举报