奥飞火千万电 发表于 2015-9-25 07:49:49

sharepoint Lists Web service 用法

  概述
  在sharepoint 项目中,后期做数据迁移时,会用到sharepoint的web service来完成把数据导入sharepoint站点的功能。
  web service 名称:
  http:///_vti_bin/Lists.asmx
  
  我们用它来新增,修改或者删除当前站点特定list 的item操作。
  调用的方法:

public XmlNode UpdateListItems (
string listName,
XmlNode updates
)
  
  
  listName:当前站点list的名字。
  操作list的item则通过XmlNode来完成。
  更改普通list里item对应field Name的xml代码:

<Batch OnError=&quot;Continue&quot; ListVersion=&quot;1&quot;
ViewName=&quot;270C0508-A54F-4387-8AD0-49686D685EB2&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Update&quot;>
<Field Name=&quot;ID&quot;>4<Field>
<Field Name=&quot;Field_Name&quot;>Value</Field>
</Method>
<Method ID=&quot;2&quot; Cmd=&quot;Update&quot;>
<Field Name=&quot;ID&quot; >6</Field>
<Field Name=&quot;Field_Name&quot;>Value</Field>
</Method>
</Batch>
  
  新增list里item的xml代码:

<Batch OnError=&quot;Continue&quot; ListVersion=&quot;1&quot;
ViewName=&quot;270C0508-A54F-4387-8AD0-49686D685EB2&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;New&quot;>
<Field Name='ID'>New</Field>
<Field Name=&quot;Title&quot;>Value</Field>
<Field Name=&quot;Date_Column&quot;>2007-3-25</Field>
<Field Name=&quot;Date_Time_Column&quot;>
2006-1-11T09:15:30Z</Field>
</Method>
</Batch>
  
  
  删除list里item的xml代码:

<Batch OnError=&quot;Continue&quot; ListVersion=&quot;1&quot;
ViewName=&quot;270C0508-A54F-4387-8AD0-49686D685EB2&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Delete&quot;>
<Field Name='ID'>2</Field>
</Method>
<Method ID=&quot;2&quot; Cmd=&quot;Delete&quot;>
<Field Name='ID'>8</Field>
</Method>
</Batch>
  
  
  document libraries操作:
  新建文件夹xml的代码:

<Batch OnError=&quot;Continue&quot; PreCalc=&quot;TRUE&quot;
ListVersion=&quot;0&quot;
ViewName=&quot;{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;New&quot;>
<Field Name=&quot;ID&quot;>New</Field>
<Field Name=&quot;FSObjType&quot;>1</Field>
<Field Name=&quot;BaseName&quot;>Name</Field>
</Method>
</Batch>
  
  
  
  更新文件夹的xml代码:

<Batch OnError=&quot;Continue&quot; PreCalc=&quot;TRUE&quot;
ListVersion=&quot;0&quot;
ViewName=&quot;{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Update&quot;>
<Field Name=&quot;ID&quot;>3</Field>
<Field Name=&quot;owshiddenversion&quot;>1</Field>
<Field Name=&quot;FileRef&quot;>
http://Server/Shared
Documents/Folder</Field>
<Field Name=&quot;FSObjType&quot;>1</Field>
<Field Name=&quot;BaseName&quot;>Name</Field>
</Method>
</Batch>
  
  
  删除文件夹xml代码:

<Batch OnError=&quot;Continue&quot; PreCalc=&quot;TRUE&quot;
ListVersion=&quot;0&quot;
ViewName=&quot;{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Delete&quot;>
<Field Name=&quot;ID&quot;>4</Field>
<Field Name=&quot;FileRef&quot;>
http://Server/Shared
Documents/Folder</Field>
</Method>
</Batch>
  
  
  更新文件夹里文件的xml代码:

<Batch OnError=&quot;Continue&quot; PreCalc=&quot;TRUE&quot;
ListVersion=&quot;0&quot;
ViewName=&quot;{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Update&quot;>
<Field Name=&quot;ID&quot;>2</Field>
<Field Name=&quot;owshiddenversion&quot;>1</Field>
<Field Name=&quot;FileRef&quot;>
http://Server/Shared
Documents/File</Field>
<Field Name=&quot;BaseName&quot;>Name</Field>
</Method>
</Batch>
  
  
  删除文件夹里文件的xml代码:

<Batch OnError=&quot;Continue&quot; PreCalc=&quot;TRUE&quot;
ListVersion=&quot;0&quot;
ViewName=&quot;{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}&quot;>
<Method ID=&quot;1&quot; Cmd=&quot;Delete&quot;>
<Field Name=&quot;ID&quot;>3</Field>
<Field Name=&quot;FileRef&quot;>
http://Server/Shared
Documents/File</Field>
</Method>
</Batch>
  
  
  调用方法:

Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
string strBatch = &quot;<Method ID='1' Cmd='Update'>&quot; +
&quot;<Field Name='ID'>4</Field>&quot; +
&quot;<Field Name='Field_Number'>999</Field></Method>&quot; +
&quot;<Method ID='2' Cmd='Update'><Field Name='ID' >6</Field>&quot; +
&quot;<Field Name='Field_DateTime'>
2003-11-11T09:15:30Z</Field></Method>&quot;;
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement(&quot;Batch&quot;);
elBatch.SetAttribute(&quot;OnError&quot;,&quot;Continue&quot;);
elBatch.SetAttribute(&quot;ListVersion&quot;,&quot;1&quot;);
elBatch.SetAttribute(&quot;ViewName&quot;,
&quot;0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40&quot;);
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems(&quot;List_Name&quot;, elBatch);
MessageBox.Show(ndReturn.OuterXml);
  
  
  返回XmlNode代码的格式:

<Results xmlns=&quot;http://schemas.microsoft.com/sharepoint/soap/&quot;>
<Result ID=&quot;1,Update&quot;>
<ErrorCode>0x00000000</ErrorCode>
<z:row ows_ID=&quot;4&quot; ows_Title=&quot;Title&quot;
ows_Modified=&quot;2003-06-19 20:31:21&quot;
ows_Created=&quot;2003-06-18 10:15:58&quot;
ows_Author=&quot;3;#User1_Display_Name&quot;
ows_Editor=&quot;7;#User2_Display_Name&quot; ows_owshiddenversion=&quot;3&quot;
ows_Attachments=&quot;-1&quot;
ows__ModerationStatus=&quot;0&quot; ows_LinkTitleNoMenu=&quot;Title&quot;
ows_LinkTitle=&quot;Title&quot;
ows_SelectTitle=&quot;4&quot; ows_Order=&quot;400.000000000000&quot;
ows_GUID=&quot;{4962F024-BBA5-4A0B-9EC1-641B731ABFED}&quot;
ows_DateColumn=&quot;2003-09-04 00:00:00&quot;
ows_NumberColumn=&quot;791.00000000000000&quot;
xmlns:z=&quot;#RowsetSchema&quot; />
</Result>
<Result ID=&quot;2,Update&quot;>
<ErrorCode>0x00000000</ErrorCode>
<z:row ows_ID=&quot;6&quot; ows_Title=&quot;Title&quot;
ows_Modified=&quot;2003-06-19 20:31:22&quot;
ows_Created=&quot;2003-06-18 19:07:14&quot;
ows_Author=&quot;2;#User1_Display_Name&quot;
ows_Editor=&quot;6;#User2_Display_Name&quot; ows_owshiddenversion=&quot;4&quot;
ows_Attachments=&quot;0&quot; ows__ModerationStatus=&quot;0&quot;
ows_LinkTitleNoMenu=&quot;Title&quot;
ows_LinkTitle=&quot;Title&quot; ows_SelectTitle=&quot;6&quot;
ows_Order=&quot;600.000000000000&quot;
ows_GUID=&quot;{2E8D2505-98FD-4E3E-BFDA-0C3DEBE483F7}&quot;
ows_DateColumn=&quot;2003-06-23 00:00:00&quot;
ows_NumberColumn=&quot;9001.00000000000000&quot;
xmlns:z=&quot;#RowsetSchema&quot; />
</Result>
...
</Results>
  
  
  取返回值的方法:

            foreach (XmlNode node in nodes)
{
if (node.Name == &quot;rs:data&quot;)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes.Name == &quot;z:row&quot;)
{
string ID = node.ChildNodes.Attributes[&quot;ows_ID&quot;].Value;
string Title = node.ChildNodes.Attributes[&quot;ows_Title&quot;].Value;
}
}
}
}
  
  
  总结
  简单介绍了Lists.UpdateListItems (string listName,XmlNode node)的用法。
页: [1]
查看完整版本: sharepoint Lists Web service 用法