Uncategorized

Currency Exchange Web Part in SharePoint 2010 (Google API)


In this post I will provide very simple code to implement currency exchange mechanism for my client. I searched little on api’s which can provide the current currency rate as my client need to convert it to Pakistan rupees.

In this web part we will consume google api’s (http://www.google.com/ig/calculator?hl=en&q=). We send request and receive response
(1) Create a Visual WebPart.
(2) Add Namespace

#region Namespaces
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Threading;
using System.IO;
#endregion

Now Add the following aspx on aspx of usercontrol, (Remember Visual webpart is pointing to a usercontrol which reside on 14 hives or root).

h1, h2, h3, h4, h5, h6
{
font-size: 1.5em;
color: #666666;
font-variant: small-caps;
text-transform: none;
font-weight: 200;
margin-bottom: 0px;

}

p
{
margin-bottom: 10px;
line-height: 1.6em;
font-size: .80em;
font-family: “Helvetica Neue”, “Lucida Grande”, “Segoe UI”, Arial, Helvetica, Verdana, sans-serif;
margin: 0px;
padding: 0px;
color: #696969;
}

Currency Converter  to *

<h2>
Currency Converter&nbsp; to *
</h2>
<p>
(AED) Amount to Convert (AED)
<table>
<tr>
<td>Enter Amount </td>
<td>
<asp:TextBox ID=”txt_amt” runat=”server” ToolTip=”kk”></asp:TextBox>
</td>
</tr>
<tr>
<td>Select Currency (From) </td>
<td>
<asp:DropDownList ID=”drp_From_Curency” runat=”server”>
<asp:ListItem>USD</asp:ListItem>
<asp:ListItem>PKR</asp:ListItem>
<asp:ListItem>GBP</asp:ListItem>
<asp:ListItem>AED</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Select Currency (To Convert)</td>
<td>
<asp:DropDownList ID=”drp_To_Currency” runat=”server”>
<asp:ListItem>USD</asp:ListItem>
<asp:ListItem>PKR</asp:ListItem>
<asp:ListItem>GBP</asp:ListItem>
<asp:ListItem>AED</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>

<td colspan=”2″>
<asp:Button ID=”Button1″ runat=”server” Text=”Convert” Height=”30px”
onclick=”Button1_Click” Width=”69px” />
</td>
</tr>
<tr>

<td colspan=”2″>
<asp:Label ID=”Label1″ runat=”server” Text=”lbl_result” Visible=”False”
ToolTip=”l_”></asp:Label>
</td>
</tr>
</table>

</p>

(AED) Amount to Convert (AED)

(4) Click on the button, and Paste on the button click event
protected void Button1_Click(object sender, System.EventArgs e)
{
try
{
string Amount =txt_amt.Text.ToString();
string CurrencyTo = drp_To_Currency.SelectedItem.Text.ToString(); //DropDownList1.SelectedItem.Text.ToString();
string CurrencyFrom = drp_From_Curency.SelectedItem.Text.ToString();
string _retult = ConvertCurrency(CurrencyFrom, Amount, “”, CurrencyTo);
Label1.Visible = true;
Label1.Text = _retult;
}
catch (Exception ex)
{ }
}
(5) Add the Following Function which having the google api, you can also add values on webconfig, you can check my previous blog how to modify and use webconfig in sharepoint.

public static string ConvertCurrency(string From, string Amount, string Request, string To)
{
try
{
string Expression = Amount + From + “=?” + To;
string url = “http://www.google.com/ig/calculator?hl=en&q=&#8221; + Expression;
string response = “”;
string responseMsg = “”;// Request.Params.ToString();
string post = responseMsg;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = “POST”;
req.ContentType = “application/x-www-form-urlencoded”;
req.ContentLength = post.Length;
StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
writer.Write(post);
writer.Close();
StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
response = reader.ReadToEnd();
reader.Close();
char[] cChar = new char[3];
string[] _params = new string[100];

cChar[0] = ‘,’;
_params = response.Split(cChar[0]);
string ConvertedAmount = “”;

ConvertedAmount = _params[1];
ConvertedAmount = ConvertedAmount.Replace(“\””, “”);
ConvertedAmount = ConvertedAmount.Replace(“rhs”, “”);
ConvertedAmount = ConvertedAmount.Replace(“:”, “”);
ConvertedAmount = ConvertedAmount.Trim();
ConvertedAmount = ConvertedAmount.Remove(ConvertedAmount.IndexOf(‘ ‘), ConvertedAmount.Length – ConvertedAmount.IndexOf(‘ ‘));

return ConvertedAmount;
}
catch (Exception ex)
{

return null;

}

}

Now Deploy the Project.

Check the screen shot.

CurrecnyExchange WebpartResult

by this simple method we can integrate any api in SP 2010.

Standard

2 thoughts on “Currency Exchange Web Part in SharePoint 2010 (Google API)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s