Pregunta

I want to get the contents of a Sharepoint 2007 list via web service. I'm using this code, which I mostly copied from 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);
            }
        }
    }
}

The call to CheckOutFile() works correctly. But the GetListItems() call gives me this error:

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

I don't understand why CheckOutFile() succeeds but GetListItems() fails, especially because the document that I'm checking out is in the list that's being accessed by GetListItems().

No hay solución correcta

Otros consejos

UPDATE: This worked in a test console app, but not in my main application. Leaving this answer up for now, but I'm not going to accept it until I get this fixed.


It turned out the problem was with the URL of the web service. Even though I had added it as:

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

the app.config file had it listed as:

http://sharepoint/_vti_bin/Lists.asmx

This remained a problem even after I deleted the reference and re-added it; I had to change the app.config contents manually. Once I'd done so, the GetListItems() call succeeded.

You must set a user and password who can get the items in the library/list this way (for me it works, you could try):

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>");
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top