Danushka Silva

It's all about SharePoint

Currency Converter web part – SharePoint 2007

Recently one of our customers wanted a currency converter on their SharePoint site. This can be done developing a custom web part. I developed a web part using VS 2008. So I thought of publishing it with the source then anyone can download and modify it as they want.

Here is what it looks like

ExSc1

You can download the source codes here.

First I want to thank Shalvin for the free currency converter web service. Here he has explained how we can use the web service in SharePoint. Now I am going to explain my web part with some screenshots.

Now open your VS2008 and create a new project using Web Part template.

ExSc2

We start adding the web service to our project. Right click you project in the solution explorer and click on Add Web Reference. Type our web service URL in the URL box and click Go. If you want you can change the Web Reference Name and click Add Reference.

ExSc3

Open your .cs file and define some variables and controls.

CurrencyConvertor.CurrencyConvertor cc = new CurrencyConvertor.CurrencyConvertor();
double dblConv;
double dblAmt;
DropDownList lstEndCurr;
DropDownList lstStartCurr;
Button btnSend;
Label lblConversion;
TextBox txtAmount;
Label lblConvert;
Label lblInto;
Label lblAmount;
Label lblDetails;

Make sure you have “WebControls” namespace in your class

using System.Web.UI.WebControls;

We are going to have two DropDownList controls for “From” and “To” fields. So we need to fill them with currency codes with description. So here I have wrote a function for that which will return a HashTable and you can directly bind that to the DropDownList. Here if you want you can directly call method from the web service and fill the DropDoenList.

private Hashtable setDDL()
{
    Hashtable hTable = new Hashtable();
    hTable.Add("AFA", "AFA-Afghanistan Afghani");
    hTable.Add("ALL", "ALL-Albanian Lek");
    hTable.Add("DZD", "DZD-Algerian Dinar");

    return hTable;
}

Then we need to add our controls to a collection. So we can call them when we render the page. So here we use enumeration type.

private enum WebPartControls
{ lblAmount, txtAmount, lblConvert, lstStartCurr, lblInto, lstEndCurr, btnSend, lblConversion, lblDetails }

Now you can add code for all your controls to the “CreateChildControls” method. Here I haven’t add all my codes in this post.

protected override void CreateChildControls()
{
    //Set properties for Amount label
    lblAmount = new Label();
    lblAmount.Text = "Amount :";
    lblAmount.Width = 60;
    this.Controls.Add(lblAmount);

    //Set properties for Amount textbox
    txtAmount = new TextBox();
    txtAmount.Width = 100;
    this.Controls.Add(txtAmount);

    //Set properties and items for from DropDownList
    lstStartCurr = new DropDownList();
    //Define a Hashtable and set the return Hashtable from setDDL function
    Hashtable hTableStart = new Hashtable();
    hTableStart = setDDL();
    lstStartCurr = new DropDownList();
    lstStartCurr.DataSource = hTableStart;
    lstStartCurr.DataTextField = "Value";
    lstStartCurr.DataValueField = "Key";
    lstStartCurr.DataBind();
    this.Controls.Add(lstStartCurr);

    //Set properties and event for convert button
    btnSend = new Button();
    btnSend.Click += new EventHandler(btnSend_Click); 
    btnSend.Text = "Convert";
    btnSend.Width = 75;
    this.Controls.Add(btnSend);

    ChildControlsCreated = true;
}

Next we can start writing the event for our convert button

void btnSend_Click(object sender, EventArgs e)
{
//Creat two object from the currency web service
CurrencyConvertor.Currency fromCurrency = new CurrencyConvertor.Currency(); CurrencyConvertor.Currency toCurrency = new CurrencyConvertor.Currency();
//Convert selected items from DropDownLists into currency objects
fromCurrency = (CurrencyConvertor.Currency)Enum.Parse(typeof(CurrencyConvertor.Currency),
this.lstStartCurr.SelectedValue.ToString()); toCurrency = (CurrencyConvertor.Currency)Enum.Parse(typeof(CurrencyConvertor.Currency),
this.lstEndCurr.SelectedValue.ToString());


//Pass our from and to currency codes to web service and get the return value to a variable
dblConv = cc.ConversionRate(fromCurrency, toCurrency); lblConversion = new Label(); dblAmt = Double.Parse(txtAmount.Text.ToString()) * dblConv; lblConversion.Text = dblAmt.ToString(); this.Controls.Add(lblConversion);
//Set the value for Details label with the return values lblDetails = new Label(); lblDetails.Text = "1.00 " + lstStartCurr.SelectedValue.ToString() + " = " + dblConv.ToString()
+ " " + lstEndCurr.SelectedValue.ToString(); lblDetails.Style["text-align"] = "center"; this.Controls.Add(lblDetails); }

Next we can start out render method. This is the method which will create UI in our web part. We can insert html break tags between controls to have space in-between.

protected override void Render(HtmlTextWriter writer)
{
     try
     {
          Controls[(int)WebPartControls.lblAmount].RenderControl(writer);
          Controls[(int)WebPartControls.txtAmount].RenderControl(writer);
          writer.Write("<BR>");
          writer.Write("<BR>");

          if (Controls.Count == 9)
          {
               Controls[(int)WebPartControls.lblDetails].RenderControl(writer);
          }
     }
     catch(Exception ex)
     {
}

Now we are done all coding things. Select your project and go to properties. It will open the project property window. Click on Debug and click on Start browser with URL. Type your site URL in the text box.

ExSc4

Now go to Build menu and select Deploy Solution. Visual studio will create our web part and deploy it into the SharePoint site.

Happy coding!