我想通过Web服务获取Sharepoint 2007列表的内容。我正在使用以下代码,主要是从the MSDN page for GetListItems复制的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace testGetListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            sharepoint.Lists listService = new sharepoint.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            XmlDocument xmlDoc = new System.Xml.XmlDocument();

            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

            ndQueryOptions.InnerXml =
                "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
                "<DateInUtc>TRUE</DateInUtc>";
            ndViewFields.InnerXml = "<FieldRef Name='Field1' />    <FieldRef Name='Field2'/>";
            ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Field1'/>" +
                "<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Field2'/>" +
                "<Value Type=        'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>";
            try
            {
                bool checkoutResult=listService.CheckOutFile("http://sharepoint/sites/mysite/myFile.xlsx", "false", null);
                XmlNode ndListItems =
                    listService.GetListItems("Test List", null, ndQuery,
                    ndViewFields, null, ndQueryOptions, null);
            }

            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" +
                    ex.Detail.InnerText +
                     "\nStackTrace:\n" + ex.StackTrace);
            }
        }
    }
}


CheckOutFile()的调用正常工作。但是GetListItems()调用给了我这个错误:

An unhandled exception of type 'System.Net.WebException' occurred in System.Web.Services.dll
Additional information: The request failed with HTTP status 401: Unauthorized.


我不理解为什么CheckOutFile()成功但GetListItems()失败的原因,特别是因为我要检出的文档在GetListItems()访问的列表中。

最佳答案

更新:这在测试控制台应用程序中有效,但在我的主应用程序中无效。现在暂时不回答这个问题,但是直到我解决了这个问题,我才接受。



原来问题出在Web服务的URL。即使我将其添加为:

http://sharepoint/sites/mySite/_vti_bin/Lists.asmx


app.config文件将其列出为:

http://sharepoint/_vti_bin/Lists.asmx


即使删除了参考并重新添加了参考,这仍然是一个问题。我必须手动更改app.config内容。完成后,GetListItems()调用成功。

09-25 17:56