SharePoint Pals
 | Sign In
How to communicate with Country Web Services using Soap Protocol in SharePoint 2010
by Mohamed Sithik 27 Feb 2013
Author
SharePoint Developer, SharePoint Frontier
Visits   
Today  :   5     Total  :    8130

In this Article I have demonstrated how to interact with Country Web Services and get their corresponding GMT Time while the Country dropdown is selected. This is achieved by using Soap Protocol in SharePoint 2010.

Step 1:

Create a new project from the Visual Studio and add Visual Web Part to it.

Step 2:

Paste the below code inside the Page

 <table>
     <tr>
 <td>
    <asp:Label ID="LblCountryName" runat="server" Text="CountryName" 
         Font-Size="Small"></asp:Label>
 </td>
 <td>
 <asp:DropDownList ID="DdlCountryName" runat="server" AutoPostBack=true 
         onselectedindexchanged="DdlCountryName_SelectedIndexChanged" 
         Font-Size="Small" ></asp:DropDownList>
 </td>
 </tr>
     <tr>
 <td>
    <asp:Label ID="LblGmtTime" runat="server" Text="GMT Time" Font-Size="Small"></asp:Label>
 </td>
 <td>
 <asp:Label ID="LblGMT" runat="server" Font-Size="Small"></asp:Label>
 </td>
     </tr>
 </table>

Step 3:

Go ahead to the coding page and add following Namespace to your project

clip_image002

Step 4:

Use the below code in your project

 protected void Page_Load(object sender, EventArgs e)
 {
 if (!Page.IsPostBack)
 {
 GetCountryName();
 }
 }
 public void GetCountryName()
 {
 string soap =
 @"<?xml version=""1.0"" encoding=""utf-8""?>
 <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
 xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
 xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
 <soap:Body>
 <GetCountriesResponse xmlns=""http://www.webserviceX.NET"">
 <GetCountriesResult>string</GetCountriesResult>
 </GetCountriesResponse>
 </soap:Body>
 </soap:Envelope>";
 string URLString = "http://www.webservicex.net/country.asmx?op=GetCountries";
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URLString);
 IWebProxy proxy = req.Proxy;
 WebProxy myProxy = new WebProxy();
 myProxy.Credentials = new NetworkCredential("[domain\username]", "[Password]");
 myProxy.BypassProxyOnLocal = true;
 req.Proxy = myProxy;
 req.Headers.Add("SOAPAction", "\"http://www.webserviceX.NET/GetCountries\"");
 req.ContentType = "text/xml;charset=\"utf-8\"";
 req.Accept = "text/xml";
 req.Method = "POST";
 using (Stream stm = req.GetRequestStream())
 {
 using (StreamWriter stmw = new StreamWriter(stm))
 {
 stmw.Write(soap);
 }
 }
 WebResponse response = req.GetResponse();
 Stream responseStream = response.GetResponseStream();
 StreamReader readStream = new StreamReader(response.GetResponseStream());
 DataSet ds = new DataSet();
 ds.ReadXml(response.GetResponseStream());
 string datas = ds.Tables[1].Rows[0].ItemArray[0].ToString();
 XmlDocument doc = new XmlDocument();
 doc.LoadXml(string.Format(datas));
 DataSet Ds = new DataSet();
 XmlNodeReader xmlread = new XmlNodeReader(doc);
 Ds.ReadXml(xmlread);
 DdlCountryName.DataTextField = "Name";
 DdlCountryName.DataSource = Ds;
 DdlCountryName.DataBind();
 }
 public void GetGmtTimebyCountry(string CountryName)
 {
 string soap =
 @"<?xml version=""1.0"" encoding=""utf-8""?>
 <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
 xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
 xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
 <soap:Body>
 <GetGMTbyCountry xmlns=""http://www.webserviceX.NET"">
 <CountryName>"+CountryName+"</CountryName></GetGMTbyCountry></soap:Body></soap:Envelope>";
 string URLString = "http://www.webservicex.net/country.asmx?op=GetGMTbyCountry";
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URLString);
 IWebProxy proxy = req.Proxy;
 WebProxy myProxy = new WebProxy();
 myProxy.Credentials = new NetworkCredential("[domain\username]", "[Password]");
 myProxy.BypassProxyOnLocal = true;
 req.Proxy = myProxy;
 req.Headers.Add("SOAPAction", "\"http://www.webserviceX.NET/GetGMTbyCountry\"");
 req.ContentType = "text/xml;charset=\"utf-8\"";
 req.Accept = "text/xml";
 req.Method = "POST";
 using (Stream stm = req.GetRequestStream())
 {
 using (StreamWriter stmw = new StreamWriter(stm))
 {
 stmw.Write(soap);
 }
 }
 WebResponse response = req.GetResponse();
 Stream responseStream = response.GetResponseStream();
 StreamReader readStream = new StreamReader(response.GetResponseStream());
 string TestingValue = readStream.ReadToEnd();
 XmlDocument doc = new XmlDocument();
 doc.LoadXml(TestingValue);
 DataSet Ds = new DataSet();
 XmlNodeReader xmlread = new XmlNodeReader(doc);
 Ds.ReadXml(xmlread);
 string gmtdatas = Ds.Tables[1].Rows[0].ItemArray[0].ToString();
 XmlDocument doc1 = new XmlDocument();
 doc1.LoadXml(gmtdatas);
 DataSet Ds1 = new DataSet();
 XmlNodeReader xmlread1 = new XmlNodeReader(doc1);
 Ds1.ReadXml(xmlread1);
 LblGMT.Text = Ds1.Tables[0].Rows[0].ItemArray[0].ToString();
 }
 protected void DdlCountryName_SelectedIndexChanged(object sender, EventArgs e)
 {
 string CountryName = DdlCountryName.SelectedItem.ToString();
 GetGmtTimebyCountry(CountryName);
 }

Step 5:

Deploy and Run the project.

clip_image004

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation