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
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("[domainusername]", "[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("[domainusername]", "[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.
Leave a comment