Talk to SharePoint using Web Services


Usually a typical SharePoint developer includes task likes that customizes SharePoint and builds custom solutions on top of the SharePoint platform. Usually to achieve this people use the SharePoint object model (Server object model).

But there are some cases where you will be working on a server where SharePoint is not installed yet you will need to get data or you need to otherwise communicate with a SharePoint platform or sites.

Well to achieve such functionalities, MOSS 2007 had only one way; by using SharePoint WebServices.

Now with the SharePoint 2010 there are many options available now such as SharePoint WebServices, Client object model, REST web services.

We will have a quick look on how we can code using SharePoint web services.

I have created a sample console application in which I am making use of SharePoint 2007 Web Services.

Typically all SharePoint web services (.asmx) are found in [12 hive]\ISAPI folder and can be accessed like http://servername/_vti_bin/webservicename.asmx

To get started I will create a sample console application and will add Web Reference of Webs.asmx (_vti_bin/Webs.asmx).

After adding this web reference we can use web service like this, I am trying to retrieve all available webs in site collection and urls.

I hope this helps someone...

static void Main(string[] args)

{

   string siteUrl = string.Empty;

   Console.WriteLine("Enter Site Name:");

   siteUrl = Console.ReadLine();

   Uri _uri = new Uri(siteUrl);

   try

   {

      WebsSvc.Webs _webs = new ConsumeWebService.WebsSvc.Webs();

      _webs.Url = siteUrl + @"/_vti_bin/Webs.asmx";

      _webs.UseDefaultCredentials = true;

              

 XmlNode _allWebs = _webs.GetAllSubWebCollection();

       string _webUrl = string.Empty;

       if (_allWebs != null && _allWebs.ChildNodes.Count > 0)

       {

         foreach (XmlNode _web in _allWebs.ChildNodes)

         {

           if (_web.Attributes["Title"] != null && _web.Attributes["Url"] != null)

           {

  Console.WriteLine(_web.Attributes["Title"].Value + " - " + _web.Attributes["Url"].Value);

           }

          }

       }

   }

   catch (Exception ex)

   {

       Console.WriteLine("Error : " + ex.Message);

   }

}