Pages

Wednesday, March 16, 2011

Talk to SharePoint using Web Services

most of the times , being a typical SharePoint developer includes task likes customizing SharePoint and build your custom solutions on top of SP platform , mostly to achieve this people use SharePoint object model (Server object model)
But there are some cases where you will not be working on server where SharePoint is not installed and still you will need to get the data or communicate to SharePoint platform or sites
Well to achieve such functionalities, MOSS 2007 had only one way and that’s by using SharePoint WebServices
Now with the SharePoint 2010 one have lot of 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 create 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 J

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);
   }
}

No comments:

Post a Comment