Thursday, October 6, 2011

How To: SharePoint 2010 Exchange Rate Web Part

This is another web part which our clients frequently require which is not always available on the web, and when it is it's not what you're looking for. The workings of this is going to be really simple so you will be able to customize it easily for your needs. The web part will for now only display selected exchange rates. The RSS feed is provided by TheMoneyConverter.com



1. Create a Visual Web Part project
In Visual Studio, click on File > New > Project and select the Visual Web Part template from the SharePoint 2010 installed templates. Give your project a name e.g. CurrencyWebPart, click Ok and click Finish.



In your solution explorer you can rename the web part and its code files to suit your needs. I have renamed mine to CurrencyWebPart. Remember to update the properties in the .webpart file if you rename your solution elements otherwise you will get an issue with the SafeControl entries when trying to add the web part to a page.

2. Add the front-end controls

The following is the code for the user control .ascx file
<style type="text/css">
    .erBox
    {
        text-align:center;
        width:300px;
    }
    .erTitle, erItem, erDate
    {
        font-size:8pt;
        font-family:Verdana, Arial, Helvetica, Sans-Serif;
        color:#676767;
    }
   
    .erTitle
    {
        font-weight:bold;
        margin-bottom:5px;
    }
   
    .erLastUpdate
    {
        margin-top:5px;
        font-weight:bold;
    }
   
    .erDate, .erLastUpdate
    {
        font-style:italic;
    }
</style>

<div class="erBox">
    <div class="erTitle"><asp:Label runat="server" ID="lblTitle"></asp:Label></div>
    <div class="erItem">1 USD = <asp:Label runat="server" ID="lblUSD"></asp:Label> ZAR</div>
    <div class="erItem">1 GBP = <asp:Label runat="server" ID="lblGBP"></asp:Label> ZAR</div>
    <div class="erItem">1 EUR = <asp:Label runat="server" ID="lblEUR"></asp:Label> ZAR</div>
    <div class="erLastUpdate">Last update on:</div>
    <div class="erDate"><asp:Label runat="server" ID="lblDate"></asp:Label></div>
</div>


3. Add the code for rendering the page

The following code will get the Xml document from the URL and transform the data for the web part.

public partial class CurrencyUserControl : UserControl
{
 public const string _rssUrl = "
http://themoneyconverter.com/ZAR/rss.xml";
 protected void Page_Load(object sender, EventArgs e)
 {
  // get the nodes
  XmlDocument xdoc = CreateXmlDoc();
  string title = xdoc.SelectSingleNode("/rss/channel/title").InnerText;
  string buildDate = xdoc.SelectSingleNode("/rss/channel/lastBuildDate").InnerText;
  XmlNodeList nodeList = xdoc.SelectNodes("/rss/channel/item");

  DisplayExchangeRates(nodeList);
  lblTitle.Text = title;
  lblDate.Text = buildDate;
 }

 private void DisplayExchangeRates(XmlNodeList nodeList)
 {
  foreach (XmlNode node in nodeList)
  {
   string title = GetTitle(node.SelectSingleNode("title").InnerText);
   double value = GetValue(node.SelectSingleNode("description").InnerText);

   if (title.Equals("USD"))
    lblUSD.Text = value.ToString("##.00");
   else if (title.Equals("GBP"))
    lblGBP.Text = value.ToString("##.00");
   else if(title.Equals("EUR"))
    lblEUR.Text = value.ToString("##.00");
  }
 }

 private string GetTitle(string title)
 {
  string val = title;
  val = val.Substring(0, val.IndexOf("/"));
  return val;
 }

 private Double GetValue(string value)
 {
  // seeing that the value is in a description we have to hack it out there... yes I know it's nasty
  string val = value;
  val = val.Substring(val.IndexOf("=") + 2);
  val = val.Substring(0, val.IndexOf(" "));
  return 1 / double.Parse(val);
 }

 private XmlDocument CreateXmlDoc()
 {
  // create a reader and populate the document
  XmlReader reader = XmlReader.Create(_rssUrl);
  XmlDocument doc = new XmlDocument();
  doc.Load(reader);
  return doc;
 }
}


4. Deploy the solution

The solution can be deployed by right clicking the project in your solution explorer and clicking Deploy.

1 comment:

  1. New feed link for currency is : http://themoneyconverter.com/rss-feed/AUD/rss.xml

    ReplyDelete