Consume SharePoint WebServices with Console Application

SharePoint can be used for a variety of different purposes. Most of the time it’s part of a bigger solution/infrastucture. If that’s the case, soon you will find yourself wanting to use SharePoint data in other applications. There are several ways of getting data out of SharePoint, we will be looking at the OOTB shipped SharePoint Webservices.

  1. First we want to create a new console application,
    Open Visual Studio and go to File → New → Project.
  2. We now want to create a new Service Reference to our SharePoint site, right click your newly created project and choose “Add Service Reference”
  3. Its time to get our hands dirty and write some code, HiHaahh
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using SharePointWebServiceDemo.SPListWS;
    using System.Xml.Linq;
    
    namespace SharePointWebServiceDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ListsSoapClient proxy = new ListsSoapClient())
                {
                    proxy.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
                    proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                    XElement spLists = proxy.GetListCollection();
                    foreach (XElement el in spLists.Descendants())
                    {
                        System.Console.WriteLine(el.Name);
                        foreach (XAttribute attrib in el.Attributes())
                        {
                            System.Console.WriteLine("> " + attrib.Name + " = " + attrib.Value);
                        }
                    }
                }
                Console.ReadKey();
            }
        }
    }
    
  4. Last, but not least, we edit our app.config
    Change:

    <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    

    Into:

    <security mode="TransportCredentialOnly">
      <transport clientCredentialType="Ntlm"/>
    </security>

Credits: Bin Yu

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s