sharepoint Lists Web service 用法
概述
在sharepoint 项目中,后期做数据迁移时,会用到sharepoint的web service来完成把数据导入sharepoint站点的功能。
web service 名称:
http://[site]/_vti_bin/Lists.asmx
我们用它来新增,修改或者删除当前站点特定list 的item操作。
调用的方法:
1 2 3 4 5 | [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)] public XmlNode UpdateListItems ( string listName, XmlNode updates ) |
listName:当前站点list的名字。
操作list的item则通过XmlNode来完成。
更改普通list里item对应field Name的xml代码:
1 2 3 4 5 6 7 8 9 10 11 | <Batch OnError= "Continue" ListVersion= "1" ViewName= "270C0508-A54F-4387-8AD0-49686D685EB2" > <Method ID= "1" Cmd= "Update" > <Field Name= "ID" >4<Field> <Field Name= "Field_Name" >Value</Field> </Method> <Method ID= "2" Cmd= "Update" > <Field Name= "ID" >6</Field> <Field Name= "Field_Name" >Value</Field> </Method> </Batch> |
新增list里item的xml代码:
1 2 3 4 5 6 7 8 9 10 | <Batch OnError= "Continue" ListVersion= "1" ViewName= "270C0508-A54F-4387-8AD0-49686D685EB2" > <Method ID= "1" Cmd= "New" > <Field Name= 'ID' >New</Field> <Field Name= "Title" >Value</Field> <Field Name= "Date_Column" >2007-3-25</Field> <Field Name= "Date_Time_Column" > 2006-1-11T09:15:30Z</Field> </Method> </Batch> |
删除list里item的xml代码:
1 2 3 4 5 6 7 8 9 | <Batch OnError= "Continue" ListVersion= "1" ViewName= "270C0508-A54F-4387-8AD0-49686D685EB2" > <Method ID= "1" Cmd= "Delete" > <Field Name= 'ID' >2</Field> </Method> <Method ID= "2" Cmd= "Delete" > <Field Name= 'ID' >8</Field> </Method> </Batch> |
document libraries操作:
新建文件夹xml的代码:
1 2 3 4 5 6 7 8 9 | <Batch OnError= "Continue" PreCalc= "TRUE" ListVersion= "0" ViewName= "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}" > <Method ID= "1" Cmd= "New" > <Field Name= "ID" >New</Field> <Field Name= "FSObjType" >1</Field> <Field Name= "BaseName" >Name</Field> </Method> </Batch> |
更新文件夹的xml代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <Batch OnError= "Continue" PreCalc= "TRUE" ListVersion= "0" ViewName= "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}" > <Method ID= "1" Cmd= "Update" > <Field Name= "ID" >3</Field> <Field Name= "owshiddenversion" >1</Field> <Field Name= "FileRef" > http: //Server/[sites/][Site/]Shared Documents/Folder</Field> <Field Name= "FSObjType" >1</Field> <Field Name= "BaseName" >Name</Field> </Method> </Batch> |
删除文件夹xml代码:
1 2 3 4 5 6 7 8 9 10 | <Batch OnError= "Continue" PreCalc= "TRUE" ListVersion= "0" ViewName= "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}" > <Method ID= "1" Cmd= "Delete" > <Field Name= "ID" >4</Field> <Field Name= "FileRef" > http: //Server/[sites/][Site/]Shared Documents/Folder</Field> </Method> </Batch> |
更新文件夹里文件的xml代码:
1 2 3 4 5 6 7 8 9 10 11 12 | <Batch OnError= "Continue" PreCalc= "TRUE" ListVersion= "0" ViewName= "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}" > <Method ID= "1" Cmd= "Update" > <Field Name= "ID" >2</Field> <Field Name= "owshiddenversion" >1</Field> <Field Name= "FileRef" > http: //Server/[sites/][Site/]Shared Documents/File</Field> <Field Name= "BaseName" >Name</Field> </Method> </Batch> |
删除文件夹里文件的xml代码:
1 2 3 4 5 6 7 8 9 10 | <Batch OnError= "Continue" PreCalc= "TRUE" ListVersion= "0" ViewName= "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}" > <Method ID= "1" Cmd= "Delete" > <Field Name= "ID" >3</Field> <Field Name= "FileRef" > http: //Server/[sites/][Site/]Shared Documents/File</Field> </Method> </Batch> |
调用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists(); listService.Credentials= System.Net.CredentialCache.DefaultCredentials; string strBatch = "<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>4</Field>" + "<Field Name='Field_Number'>999</Field></Method>" + "<Method ID='2' Cmd='Update'><Field Name='ID' >6</Field>" + "<Field Name= 'Field_DateTime' > 2003-11-11T09:15:30Z</Field></Method>"; XmlDocument xmlDoc = new System.Xml.XmlDocument(); System.Xml.XmlElement elBatch = xmlDoc.CreateElement( "Batch" ); elBatch.SetAttribute( "OnError" , "Continue" ); elBatch.SetAttribute( "ListVersion" , "1" ); elBatch.SetAttribute( "ViewName" , "0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40" ); elBatch.InnerXml = strBatch; XmlNode ndReturn = listService.UpdateListItems( "List_Name" , elBatch); MessageBox.Show(ndReturn.OuterXml); |
返回XmlNode代码的格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <Result ID= "1,Update" > <ErrorCode>0x00000000</ErrorCode> <z:row ows_ID= "4" ows_Title= "Title" ows_Modified= "2003-06-19 20:31:21" ows_Created= "2003-06-18 10:15:58" ows_Author= "3;#User1_Display_Name" ows_Editor= "7;#User2_Display_Name" ows_owshiddenversion= "3" ows_Attachments= "-1" ows__ModerationStatus= "0" ows_LinkTitleNoMenu= "Title" ows_LinkTitle= "Title" ows_SelectTitle= "4" ows_Order= "400.000000000000" ows_GUID= "{4962F024-BBA5-4A0B-9EC1-641B731ABFED}" ows_DateColumn= "2003-09-04 00:00:00" ows_NumberColumn= "791.00000000000000" xmlns:z= "#RowsetSchema" /> </Result> <Result ID= "2,Update" > <ErrorCode>0x00000000</ErrorCode> <z:row ows_ID= "6" ows_Title= "Title" ows_Modified= "2003-06-19 20:31:22" ows_Created= "2003-06-18 19:07:14" ows_Author= "2;#User1_Display_Name" ows_Editor= "6;#User2_Display_Name" ows_owshiddenversion= "4" ows_Attachments= "0" ows__ModerationStatus= "0" ows_LinkTitleNoMenu= "Title" ows_LinkTitle= "Title" ows_SelectTitle= "6" ows_Order= "600.000000000000" ows_GUID= "{2E8D2505-98FD-4E3E-BFDA-0C3DEBE483F7}" ows_DateColumn= "2003-06-23 00:00:00" ows_NumberColumn= "9001.00000000000000" xmlns:z= "#RowsetSchema" /> </Result> ... </Results> |
取返回值的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | foreach (XmlNode node in nodes) { if (node.Name == "rs:data" ) { for ( int i = 0; i < node.ChildNodes.Count; i++) { if (node.ChildNodes[i].Name == "z:row" ) { string ID = node.ChildNodes[i].Attributes[ "ows_ID" ].Value; string Title = node.ChildNodes[i].Attributes[ "ows_Title" ].Value; } } } } |
总结
简单介绍了Lists.UpdateListItems (string listName,XmlNode node)的用法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架