5 October 2011

Accessing web service in SharePoint Online (Cloud –Office 365)

Accessing web service in SharePoint Online (Cloud –Office 365).
It was an end of a tiring day where I tried to consume a web service in the SharePoint online and finally I succeeded. Thanks to Sajid Mohammad who redirected me to the blog of Wictor , and a great thanks  to him for providing  MsOnlineClaimsHelper class.Un like in  Wictor’s  example  my requirement was to consume a web service.
So All need to do is that assign my webservice’s cookicontainer class to the claims helper’s cookiecontainer as follows.
objLists.CookieContainer = claimsHelper.CookieContainer;
The whole code will look like below.
try
{
MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper("http://......./teamsite/_vti_bin/Lists.asmx", @"<username>", "<password>");
FileGET.Lists objLists = new FileGET.Lists();
objLists.CookieContainer = claimsHelper.CookieContainer;
objLists.Url = "http://dev22.sharepoint.com/TeamSite/_vti_bin/Lists.asmx";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
XmlNode ndListItems = objLists.GetListItems("test", null, null, null, null, ndQueryOptions, null);
XmlTextReader reader = new XmlTextReader(ndListItems.OuterXml, XmlNodeType.Element, null);
//to display in my windows application
DataSet ds = new DataSet();
ds.ReadXml(reader);
dataGridView1.DataSource = ds.Tables[1];
}
catch (Exception ex)
{
 MessageBox.Show(ex.ToString());
}

----------------------------------------------------------------------------------------------------------------------------
Note : In case you are calling this with in a firewall dont forget to put the following ahead of the script.

if (WebRequest.DefaultWebProxy.Credentials == null)WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;