How can I cal the lists.asmx web service?

Eliasen, Jan 46 Reputation points
2020-12-04T00:11:38.92+00:00

Hi all

I cannot seem to be able to call the lists.asmx web service (from a C# class library and could use some help.

The web service is hosted on sharepoint.com on a URL like this:
https://<company>.sharepoint.com/sites/<sitename>/_vti_bin/lists.asmx
I can browse to the URL of the site. And I can browse to the URL of the list that I actually want to retrieve data from.

I can also browse to the URL of the .asmx and I can add a service reference to it in Visual Studio.

When I add the service reference in Visual Studio, my app.config looks like this:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ListsSoap">
<security mode="Transport" />
</binding>
<binding name="ListsSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://<company>.sharepoint.com/_vti_bin/lists.asmx"
binding="basicHttpBinding" bindingConfiguration="ListsSoap"
contract="ListsASMX.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>

So the URL has changed a bit - the site name has been removed. Is this a problem? It doesn't seem to make any difference to me in my code.

So my code to call the web service looks like this:

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

            query.InnerXml = "";
            viewFields.InnerXml = "";
            queryOptions.InnerXml = "";

            ListsASMX.ListsSoapClient client = new ListsASMX.ListsSoapClient();
            try
            {
                var result = client.GetListItems("<name of list>", "", query, viewFields, "150", queryOptions, null);
            }
            catch (Exception ex)
            {
            }

But I get an exception saying that "{"The HTTP request was forbidden with client authentication scheme 'Anonymous'."}"

So I went to my app.config and changed this:

<security mode="Transport" />

to this:

<security mode="Transport">
       <transport clientCredentialType="None" />
 </security>

That gave me the same error.
Then I tried with clientCredentialType = Windows. Then the error became: "{"The HTTP request was forbidden with client authentication scheme 'Negotiate'."}"
Then I tried with clientCredentialType = Ntlm. Then the error became: "{"The HTTP request was forbidden with client authentication scheme 'Ntlm'."}"

What am I doing wrong? I would just like to be able to retrieve data from the list <Name of list> using integrated security so I don't need to specify any credentials explicitly in my code.

Any help or pointers? I cna't seem to find anything on the internet that has this exact same issue.

Thanks!

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2020-12-04T06:04:59.883+00:00

    Hi @Eliasen, Jan ,

    SharePoint Online is using OAuth authentication instead of Windows/NTLM Authentication, needs the additional access token.

    So I suggest you could use SharePoint Online CSOM instead of Lists.asmx, as Lists.asmx is always used in SharePoint Sever environment and now Lists.asmx has been deprecated:

    choose-the-right-api-set-in-sharepoint

    Back to the requirement, if want to get the list item of a list, please try the following code snippet:

            string userName = "user@Tenant.onmicrosoft.com";  
            string AppPassword = "your password";  
            var securePassword = new SecureString();  
            foreach (char c in AppPassword)  
            {  
                securePassword.AppendChar(c);  
            }  
            using (var context = new ClientContext("https://tenant.sharepoint.com/"))  
            {  
                context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);  
                Web web = context.Web;  
                context.Load(web);  
                context.Load(context.Web.Folders);  
                context.ExecuteQuery();  
                List list = web.Lists.GetByTitle("MyList");  
                ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());  
                context.Load(items);  
                context.ExecuteQuery();  
    
            }  
    

    SharePoint Online CSOM can be download and install using Nuget:

    Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.20720.12000

    Microsoft.SharePointOnline.CSOM


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Trevor Seward 11,696 Reputation points
    2020-12-05T00:10:29.957+00:00

    Lists.asmx is deprecated. Use REST API calls instead.

    You should also be using the Client ID/Secret (SharePoint Addin) or Graph API model and not username/password.