【问题标题】:How do I use the Sharepoint Web Service to get a list of documents from .NET?如何使用 Sharepoint Web 服务从 .NET 获取文档列表?
【发布时间】:2011-09-28 05:49:58
【问题描述】:

技术: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

【问题讨论】:

    标签: c# .net winforms sharepoint sharepoint-2007


    【解决方案1】:

    在我的工作中,我需要做一些类似于 Harmon.ie 的 SharePoint add-on 的事情。他们拥有一整套集成 SharePoint 和 Outlook 的功能。我只需要将 Outlook 中的文档作为侧边栏列出,所以我尝试自己开发它。
    我尝试改编开源代码论坛this project,但最终超出了我的深度。 你可以参考一下,相信对你有帮助。

    【讨论】:

      【解决方案2】:

      不确定是否需要,但有 不仅仅是一个共享点站点 托管在“website.com”(我是 试图访问是 “website.com/sites/mysite”)这样做 改变什么?

      确实如此!您发现的 Web 服务 URL 实际上是虚拟的,这意味着 URL 模式 _vti_bin/Lists.asmx 在每个 SharePoint 站点下都可用。您的列表所在的任何站点都必须是您的 Web 服务 URL 的起点。

      因此,请尝试:http://website.com/sites/mysite/_vti_bin/Lists.asmx

      您可能会发现 MSDN 的 SharePoint Web Service Guidelines 文章也很有帮助。


      更新 #1:

      CAML 不会帮助您“分解”您的结果。它严格来说是一种查询/过滤技术,看起来您使用得当。您可以告诉我们您返回的结果集是否符合您的预期。如果您在构建更复杂的 CAML 查询时需要帮助,我建议您下载一份 U2U's CAML Query Builder(但这是另一个主题)。

      所以,关于结果。看起来有点令人困惑,但疯狂是有一些方法的。您在列表中设置的列都在这里(前提是您没有在 CAML 查询中限制它们,这是可能的)。您会注意到他们使用 SharePoint 的 internal 列名称,前缀为 ows_。因此,如果您知道内部名称,则可以构造 XML 属性名称并使用 XML 类、LINQ 或 XSLT 深入研究结果并获得所需的内容。见my answer to another SO question for tips on divining SP's internal column names

      如果知道我在这种情况下做什么有帮助,我只需构建一个POCO 模型类来表示我的一个列表项,并编写一个解析器方法来获取 SharePoint 的 XML 结果并返回模型对象的集合.我喜欢 LINQ to XML 来完成这项任务。

      如果您使用的是 SharePoint 2010,则可以使用新的 Client Object Model classes 并获得更好的托管包装器来处理 SharePoint 站点、列表、ListItems 等。


      更新 #2:

      为了一些颜色,我发布了一个用于解析 SharePoint XML 的代码示例。在此示例中,Registrant 是我编写的 POCO 模型类,用于表示自定义列表的 SharePoint 列表项。这是微不足道的,我不会费心发布它。

      /// <summary>
      /// Parses registrant XML returned from SharePoint's Lists web service into a collection 
      /// of <see cref="Registrant"/> objects (root element = "listitems").
      /// </summary>
      /// <param name="xml">SharePoint XML</param>
      /// <returns>Collection of Registrant objects, or null if no registrant data could be parsed.</returns>
      public static List<Registrant> ParseSharePointXmlCollection( XElement xml ) {
      
        // Test: Not expected XML element or has no child elements
        if ( !xml.Name.LocalName.Equals( "listitems" ) || !xml.HasElements ) {
          return null;
        }
      
        List<Registrant> regList = null;
      
        XElement data = xml.Element( XName.Get( "data", "urn:schemas-microsoft-com:rowset" ) );
        if ( (data != null) && (data.HasElements) ) {
      
          regList = new List<Registrant>();
      
          IEnumerable<XElement> regXmlNodes = data.Elements( XName.Get( "row", "#RowsetSchema" ) );
          foreach (XElement regXml in regXmlNodes) {
            Registrant reg = ParseSharePointXml( regXml );
            if ( reg != null ) {
              regList.Add( reg );
            }
          }
        }
      
        return regList;
      }
      
      /// <summary>
      /// Parses registrant XML returned from SharePoint's Lists web service into a single 
      /// <see cref="Registrant"/> object (root element = "row").
      /// </summary>
      /// <param name="xml">SharePoint XML</param>
      /// <returns>A Registrant object, or null if no registrant data could be parsed.</returns>
      public static Registrant ParseSharePointXml( XElement xml ) {
      
        // Test: Not expected XML element or has no attributes
        if ( !xml.Name.LocalName.Equals( "row" ) || !xml.HasAttributes ) {
          return null;
        }
      
        Registrant reg = null;
      
        // Parse ID (if this fails, fail the whole operation)
        if ( xml.Attribute( "ows_ID" ) != null ) {
          reg = new Registrant();
          reg.ID = xml.Attribute( "ows_ID" ).Value;
        }
        else {
          return null;
        }
      
        // Parse First Name
        if ( xml.Attribute( "ows_Q_Registrant_x0020_First_x0020_N" ) != null ) {
          reg.FirstName = xml.Attribute( "ows_Q_Registrant_x0020_First_x0020_N" ).Value;
        }
      
        // Parse Last Name
        if ( xml.Attribute( "ows_Q_Registrant_x0020_Last_x0020_Na" ) != null ) {
          reg.LastName = xml.Attribute( "ows_Q_Registrant_x0020_Last_x0020_Na" ).Value;
        }
      
        // Parse Email
        if ( xml.Attribute( "ows_Q_Registrant_x0020_Email" ) != null ) {
          reg.Email = xml.Attribute( "ows_Q_Registrant_x0020_Email" ).Value;
        }
      
        // Parse Assistant Name
        if ( xml.Attribute( "ows_Q_Asst_x0020_First_x0020_Name" ) != null ) {
          reg.AssistantFirstName = xml.Attribute( "ows_Q_Asst_x0020_First_x0020_Name" ).Value;
        }
        if ( xml.Attribute( "ows_Q_Asst_x0020_Name" ) != null ) {
          reg.AssistantLastName = xml.Attribute( "ows_Q_Asst_x0020_Name" ).Value;
        }
      
        // Parse Assistant Email
        if ( xml.Attribute( "ows_Q_Asst_x0020_Email" ) != null ) {
          reg.AssistantEmail = xml.Attribute( "ows_Q_Asst_x0020_Email" ).Value;
        }
      
        return reg;
      }
      

      更新 #3:

      XmlNode 对象转换为XElement 的示例代码:

      public static XElement GetXElement( this XmlNode node ) {
        XDocument xdoc = new XDocument();
        using ( XmlWriter xmlWriter = xdoc.CreateWriter() ) {
          node.WriteTo( xmlWriter );
        }
        return xdoc.Root;
      }
      

      【讨论】:

      • 这是一个很好的开始,我之前尝试过这样做,但我没有想到要更改 .URL 属性,因为 app.config 中的值从未改变。所以我能够获取列表,但我返回的数据没有显示文档库“数据表”视图中列出的任何值。
      • 您是否使用 CAML 查询返回信息?如果是这样,请发布它(以及您如何构建它/调用 Lists Web 服务的上下文代码)。如果这些都没有意义,那么我有一些资源可以指点给你。
      • 我已经编辑了我的原始帖子以反映我的进度以及我遇到的问题,但不可否认,CAML 令人困惑,我不确定如何处理它。我知道 SO 不是入门课程,但这将是我唯一一次必须编写应用程序来与 sharepoint 交互,所以我只想完成它。
      • 嗯,所以我想我得到了我想要的,我只是很难把它拆开。我一直在尝试使用 .Attributes["Deal_x0020_ID"].Value 但每次我这样做时都说它没有任何属性。 (ps:由于您的帮助很大,所以我投票并回答了您的问题)
      • 如果我不得不猜测,这是一个 XML 命名空间问题。在我的 LINQ 代码中,我已经指定了它们。例如,要获得对 data 元素的引用,我写:XElement data = xml.Element( XName.Get("data", "urn:schemas-microsoft-com:rowset" )); 但是,如果您尝试访问 XML,请查找允许指定命名空间的重载或方法。
      猜你喜欢
      • 2011-01-05
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多