GetListItems()导致401错误,但Checkout()工作

时间:2014-03-03 20:11:18

标签: c# web-services sharepoint sharepoint-2007

我想通过网络服务获取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()访问的列表中。

2 个答案:

答案 0 :(得分:4)

更新:这适用于测试控制台应用程序,但不适用于我的主应用程序。暂时留下这个答案,但在我解决之前我不会接受它。


事实证明,问题在于网络服务的网址。即使我把它添加为:

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

app.config 文件将其列为:

http://sharepoint/_vti_bin/Lists.asmx

即使我删除了引用并重新添加它,这仍然是一个问题;我必须手动更改 app.config 内容。一旦我这样做,GetListItems()电话就成功了。

答案 1 :(得分:0)

您必须设置一个用户和密码才能以这种方式获取库/列表中的项目(对我而言,您可以尝试):

public string GetIDFromList(string parameter)
    {
        string retorno = "";

        try
        {
            string path = "http://www.test.com/web/";

            // Reference to the SharePoint Lists web service:
            WSSharePointCSCLists.Lists listsWS = new WSSharePointCSCLists.Lists();

            listsWS.Url = path + "_vti_bin/lists.asmx";

            listsWS.Credentials = GetUserCredential();

            string listName = "MyList";
            string viewName = "";
            //string webID;
            string rowLimit = "500";

            // Web ID:
            webID = "098304-9098asdf-qwelkfj-eoqiula";                    


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

            // Query em CAML (SharePoint):
            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>" +
                        "<ViewAttributes Scope=\"RecursiveAll\" />";

            ndViewFields.InnerXml = @"<FieldRef Name='Title' />                        
                                        <FieldRef Name='ID' />";


            string caml =
                String.Format(
                            @"<Where>
                                <Contains>
                                    <FieldRef Name='MyColumn' />
                                    <Value Type='Text'>{0}</Value>           
                                </Contains>                                    
                            </Where>",
                    parameter);

            ndQuery.InnerXml = caml;

            XmlNode retornoWS = listsWS.GetListItems(listName, null, ndQuery,
                                                         ndViewFields, rowLimit,
                                                         ndQueryOptions, webID);

            retorno = retornoWS.InnerXml;


        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception: " + ex.Message);
            throw;
        }

        return retorno;
    }


    public NetworkCredential GetUserCredential()
    {
        return new System.Net.NetworkCredential("<username>",
                                                "<password>",
                                                "<domain>");
    }