如何使用 Sharepoint Web 服务从 .NET 获取文档列表?
技术:Winforms、.NET、C#、Sharepoint Web 服务
我目前正在编写一个 winform 桌面应用程序,该应用程序应该访问 Sharepoint 文档和自定义输入变量的特定列表(来自 Sharepoint 文档库)
http://www.infoq.com/articles/swanson-moss-web-services
该网站提供了我对可用的网络服务有很好的了解,但我无法从这里弄清楚如何访问我想要的服务。
使用 website.com/_vti_bin/Lists.asmx 和 website.com/_vti_bin/Dws.asmx 作为我的网络参考。
com.website.Lists splist = new Project.com.website.Lists();
com.website1.Dws spData = new Project.com.website1.Dws();
splist.Credentials = new NetworkCredential("username", "password", "domain");
splist.GetList("My_list");
这是我第一次尝试连接,它给了我一个 SOAPException(这是我能提供的详细信息的范围)
不确定是否需要这样做,但有多个共享点站点托管在“website.com”(该站点)我正在尝试访问“website.com/sites/mysite”)这会改变什么吗?
我只是想知道我在使用此服务时是否走在正确的轨道上,或者我是否完全错误
编辑1:留下原来的问题,以便人们了解发生了什么。
@CBono:您链接的文章对于入门有很大帮助。 (我还不能投票:()我已经设法获得了列表,但是格式化或获取数据被证明是困难的。 以下是提供的内容:
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + "<Value Type=\"Counter\">3</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
但我的结果如下所示:
<rs:data ItemCount="4" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="" ows_Deal_x0020_ID="4055.00000000000" ows_MetaInfo="8;#Subject:SW| Deal ID:DW|4055.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2008-10-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="8;#2011-07-04 16:21:56" ows_Quarter_x002d_End_x0020_Date="2008-10-31 00:00:00" ows_ID="8" ows_owshiddenversion="3" ows_UniqueId="8;#{6594AD67-6954-4641-BDA5-8FECEDCCEAD8}" ows_FSObjType="8;#0" ows_Created_x0020_Date="8;#2011-07-04 16:20:00" ows_ProgId="8;#" ows_FileLeafRef="8;#Deal Name Q1.doc" ows_Modified="2011-07-04 16:21:55" ows_FileRef="8;#sites/site1/deals/Deal Name Q1.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="4576.00000000000" ows_MetaInfo="9;#Subject:SW| Deal ID:DW|4576.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2011-01-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="9;#2011-07-04 16:22:37" ows_Quarter_x002d_End_x0020_Date="2011-01-31 00:00:00" ows_ID="9" ows_owshiddenversion="3" ows_UniqueId="9;#{0AD9B000-3736-4B7B-9182-57C270824B8A}" ows_FSObjType="9;#0" ows_Created_x0020_Date="9;#2011-07-04 16:22:08" ows_ProgId="9;#" ows_FileLeafRef="9;#Deal Name Q4.doc" ows_Modified="2011-07-04 16:22:37" ows_FileRef="9;#sites/site1/deals/Deal Name Q4.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="80003.0000000000" ows_MetaInfo="10;#Subject:SW| Deal ID:DW|80003.0000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|0 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2011-01-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="10;#2011-07-04 16:23:15" ows_Quarter_x002d_End_x0020_Date="2011-01-31 00:00:00" ows_ID="10" ows_owshiddenversion="3" ows_UniqueId="10;#{07543C39-F299-4085-82CE-F059257796EA}" ows_FSObjType="10;#0" ows_Created_x0020_Date="10;#2011-07-04 16:22:56" ows_ProgId="10;#" ows_FileLeafRef="10;#Deal Name Q3.doc" ows_Modified="2011-07-04 16:23:15" ows_FileRef="10;#sites/site1/deals/Deal Name Q3.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="3319.00000000000" ows_MetaInfo="11;#Subject:SW| Deal ID:DW|3319.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2009-10-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="11;#2011-07-04 16:23:55" ows_Quarter_x002d_End_x0020_Date="2009-10-31 00:00:00" ows_ID="11" ows_owshiddenversion="3" ows_UniqueId="11;#{E51AB6A9-DDE1-4F66-8E44-EDFA3D0AFF7F}" ows_FSObjType="11;#0" ows_Created_x0020_Date="11;#2011-07-04 16:23:27" ows_ProgId="11;#" ows_FileLeafRef="11;#Deal Name Q2.doc" ows_Modified="2011-07-04 16:23:55" ows_FileRef="11;#sites/site1/deals/Deal Name Q2.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
</rs:data>
我不知道如何使用 CAML 来分解它。 (另外,我使用的是 Sharepoint Server 2007,而不是 2010
Technologies: Winforms, .NET, C#, Sharepoint Web Services
I'm currently writing a winform desktop application that is supposed to access a specific list of Sharepoint documents and custom input variables (From a Sharepoint Document Library)
http://www.infoq.com/articles/swanson-moss-web-services
This website gave me a good understanding of what web services are available, but I can't figure out from here how to access what I want.
Using the website.com/_vti_bin/Lists.asmx and website.com/_vti_bin/Dws.asmx as my web references.
com.website.Lists splist = new Project.com.website.Lists();
com.website1.Dws spData = new Project.com.website1.Dws();
splist.Credentials = new NetworkCredential("username", "password", "domain");
splist.GetList("My_list");
This was my first attempt at connecting, and it gives me a SOAPException (that's the extent of the details I can give)
Not sure if this is needed, but there are more that one sharepoint sites hosted at "website.com" (The one I'm trying to access is "website.com/sites/mysite" ) Does that change things?
I'm just trying to find out if I'm on the right track on using this service, or if I've got it all wrong
EDIT 1: leavig the original question so people understand what's going on.
@CBono: The article you linked was a great help on getting started on getting started. (I can't upvote yet :( ) I've managed to get the list, but formatting, or getting AT the data is proving difficult.
Here's what was provided:
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + "<Value Type=\"Counter\">3</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
But my results looks like this:
<rs:data ItemCount="4" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="" ows_Deal_x0020_ID="4055.00000000000" ows_MetaInfo="8;#Subject:SW| Deal ID:DW|4055.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2008-10-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="8;#2011-07-04 16:21:56" ows_Quarter_x002d_End_x0020_Date="2008-10-31 00:00:00" ows_ID="8" ows_owshiddenversion="3" ows_UniqueId="8;#{6594AD67-6954-4641-BDA5-8FECEDCCEAD8}" ows_FSObjType="8;#0" ows_Created_x0020_Date="8;#2011-07-04 16:20:00" ows_ProgId="8;#" ows_FileLeafRef="8;#Deal Name Q1.doc" ows_Modified="2011-07-04 16:21:55" ows_FileRef="8;#sites/site1/deals/Deal Name Q1.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="4576.00000000000" ows_MetaInfo="9;#Subject:SW| Deal ID:DW|4576.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2011-01-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="9;#2011-07-04 16:22:37" ows_Quarter_x002d_End_x0020_Date="2011-01-31 00:00:00" ows_ID="9" ows_owshiddenversion="3" ows_UniqueId="9;#{0AD9B000-3736-4B7B-9182-57C270824B8A}" ows_FSObjType="9;#0" ows_Created_x0020_Date="9;#2011-07-04 16:22:08" ows_ProgId="9;#" ows_FileLeafRef="9;#Deal Name Q4.doc" ows_Modified="2011-07-04 16:22:37" ows_FileRef="9;#sites/site1/deals/Deal Name Q4.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="80003.0000000000" ows_MetaInfo="10;#Subject:SW| Deal ID:DW|80003.0000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|0 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2011-01-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="10;#2011-07-04 16:23:15" ows_Quarter_x002d_End_x0020_Date="2011-01-31 00:00:00" ows_ID="10" ows_owshiddenversion="3" ows_UniqueId="10;#{07543C39-F299-4085-82CE-F059257796EA}" ows_FSObjType="10;#0" ows_Created_x0020_Date="10;#2011-07-04 16:22:56" ows_ProgId="10;#" ows_FileLeafRef="10;#Deal Name Q3.doc" ows_Modified="2011-07-04 16:23:15" ows_FileRef="10;#sites/site1/deals/Deal Name Q3.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
<z:row ows_Title="" ows_Deal_x0020_ID="3319.00000000000" ows_MetaInfo="11;#Subject:SW| Deal ID:DW|3319.00000000000 vti_parserversion:SR|12.0.0.6545 _Category:SW| SVP Approved?:IW|1 vti_author:SR|DD\\admin vti_approvallevel:SR| vti_categories:VW| vti_modifiedby:SR|DD\\admin vti_assignedto:SR| Keywords:SW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject Deal\\ ID vti_assignedto Keywords vti_title _Author _Category Quarter-End\\ Date ContentType SVP\\ Approved? _Comments ContentTypeId:SW|0x0101004E1C496569BCDF42912D848E8085351A ContentType:SW|Document vti_cachedtitle:SR| vti_title:SR| _Author:SW| Quarter-End Date:SW|2009-10-31T00:00:00Z _Comments:SW| " ows__ModerationStatus="0" ows__Level="1" ows_Last_x0020_Modified="11;#2011-07-04 16:23:55" ows_Quarter_x002d_End_x0020_Date="2009-10-31 00:00:00" ows_ID="11" ows_owshiddenversion="3" ows_UniqueId="11;#{E51AB6A9-DDE1-4F66-8E44-EDFA3D0AFF7F}" ows_FSObjType="11;#0" ows_Created_x0020_Date="11;#2011-07-04 16:23:27" ows_ProgId="11;#" ows_FileLeafRef="11;#Deal Name Q2.doc" ows_Modified="2011-07-04 16:23:55" ows_FileRef="11;#sites/site1/deals/Deal Name Q2.doc" ows_DocIcon="doc" ows_Editor="605;#Doe, John" xmlns:z="#RowsetSchema" />
</rs:data>
and I have nooooooo idea how I'd use CAML to break that apart. (Also, I'm using Sharepoint Server 2007, not 2010
绝对是这样!您发现的 Web 服务 URL 实际上是虚拟的,这意味着 URL 模式
_vti_bin/Lists.asmx
在每个 SharePoint 站点下都可用。无论您的列表位于哪个站点,都必须是您的 Web 服务 URL 的起点。因此,请尝试:
http://website.com/sites/mysite/_vti_bin/Lists.asmx
您可能会找到MSDN的SharePoint Web服务指南文章也很有帮助。
更新#1:
CAML 不会帮助您“分解”结果。它严格来说是一种查询/过滤技术,而且看起来您正在正确使用它。您需要告诉我们您返回的结果集是否符合您的预期。如果您需要帮助构建更复杂的 CAML 查询,我建议下载 U2U 的 CAML 查询生成器(但这是另一个主题)。
那么,看看结果。看起来有点令人困惑,但是有一些方法可以解决这种疯狂。您在列表中设置的列都在这里(前提是您没有在 CAML 查询中限制它们,这是可能的)。您会注意到,他们使用 SharePoint 的内部列名称,并以
ows_
为前缀。因此,如果您知道内部名称,则可以构造 XML 属性名称并使用 XML 类、LINQ 或 XSLT 深入研究结果并获得所需的内容。请参阅我的回答另一个关于预测 SP 内部列名称提示的 SO 问题。如果有助于了解我在这种情况下做什么,我只需构建一个 POCO 模型类来表示我的列表项之一,并编写一个解析器方法来获取 SharePoint 的 XML 结果并返回模型对象的集合。我喜欢使用 LINQ to XML 来完成这项任务。
如果您使用的是 SharePoint 2010,则可以使用新的 客户端对象模型类,并获得更好的管理包装器来处理 SharePoint 网站、列表、ListItems 等。
更新#2:
只是为了一些颜色,我发布了用于解析 SharePoint XML 的代码示例。本示例中的
Registrant
是我编写的 POCO 模型类,用于表示自定义列表的 SharePoint 列表项。这很琐碎,我不会费心发布它。更新 #3:
将
XmlNode
对象转换为XElement
的示例代码:Absolutely it does! The web service URLs you have discovered are actually virtual, meaning that the URL pattern
_vti_bin/Lists.asmx
is available under EVERY SharePoint site. Whatever site your list lives in must be the starting point for your web service URL.Thus, try:
http://website.com/sites/mysite/_vti_bin/Lists.asmx
You might find MSDN's SharePoint Web Service Guidelines article helpful, too.
UPDATE #1:
CAML won't help you "break apart" your results. It's strictly a query/filter technology, and it looks like you're using it appropriately. It's up to you to tell us whether or not the result set you're getting back is what you expect. If you need help constructing more complicated CAML queries, I suggest downloading a copy of U2U's CAML Query Builder (but that's another topic).
So, on to the results. It's a little confusing to look at it, but there is some method to the madness. The columns you've set up on your list are all here (provided you're not constraining them in the CAML query, which is possible). You'll notice they use SharePoint's internal name for the column, prefixed with
ows_
. So, if you know the internal names, you can construct the XML attribute names and use XML classes, LINQ, or XSLT to dive into the results and get what you need. See my answer to another SO question for tips on divining SP's internal column names.If it helps to know what I do in this situation, I simply build a POCO model class to represent one of my list items, and write a parser method to take SharePoint's XML results and return a collection of the model objects. I'm fond of LINQ to XML for this task.
Were you using SharePoint 2010, you could use the new Client Object Model classes and get better managed wrappers around working with SharePoint Sites, Lists, ListItems and such.
UPDATE #2:
Just for some color, I'm posting a sample of the code I use to parse SharePoint XML.
Registrant
in this example is a POCO model class I wrote to represent a SharePoint list item for a custom list. It's trivial and I won't bother posting it.UPDATE #3:
Sample code to convert an
XmlNode
object to anXElement
: