Expression Evaluation Time: ms / Round trip: ms
How to build a currency converter with MatchPoint
15. Aug
2011
by Stefan Category:
Technical
0

In this blog post I'll show you how to build a currency converter with MatchPoint. All we need is a MatchPoint Form Web Part, a MatchPoint Composite Web Part and access to a currency converter web service. In this example we'll use the web service provided by WebServiceEx (http://www.webservicex.net/CurrencyConvertor.asmx).

This web service takes two currency codes as input and returns the conversion rate. We use the Form Web Part to provide the input values. The Composite Web Part accesses these values via the Connection Framework, passes them to the web service and displays the results.

Form Web Part

The Form Web Part configuration is quite simple. It consists of two Choice Fields which provide the currencies that you can choose from. We assume the available currencies and the corresponding codes are stored in a SharePoint list Currencies. We use this list as a source for the Choice Fields. However, the values could come from other sources like a database, another web service, etc.

  1. Open the configuration and select the node Fields.
  2. Add a ChoiceField and set the Name to "From".
  3. Add a SPListChoiceProvider and set the ListUrl to the currencies list.
  4. Set the KeyColumn to the column on the list where the currency code is stored.
  5. Set the TextColumn to a descriptive column if available. Otherwise just use the same as in the KeyColumn.
  6. Add a second ChoiceField with the same configuration and set the Name to "To".

Composite Web Part

The Composite Web Part uses an Expression Data Provider to call the web service. In order for this to work we use the method CallWebService provided by the Expression Variable WSUtility. This method is able to build a proxy object on the fly if the web service provides a description in WSDL. This proxy exposes all methods that are defined by the web service. This makes using the web service easy, since the web service calls are like any other expression method calls:

WSUtility.CallWebService("http://wdsl/enabled/web/service.aspx")
  .DoSomething(withThat, andThat)

The currency converter web service provides a method ConversionRate(fromCode, toCode). This method takes currency codes as input. We now need to write an expression that calls the conversion method with the values selected in the Choice Fields on the Form Web Part. Cross Web Part communication is done with the Connection Framework. We can access the selected values in our Choice Fields with the expression ConnectionData.From and ConnectionData.To. This is where From and To correspond to the names of the fields. Choice Fields always provide their values as arrays of IChoiceItem: Therefore, to access the currency code we have to extract the Key of the first choice item:

ConnectionData.ChoiceFieldName.First().Key

The final expression of the ExpressionDataProvider looks like this:

WSUtility
  .CallWebService("http://www.webservicex.net/CurrencyConvertor.asmx")
  .ConversionRate(
    ConnectionData.From.First().Key, 
    ConnectionData.To.First().Key)

This expression will throw an exception if no currencies are selected. Thus we set the IgnoreErrors flag to true in order to return an empty result set on error.

To display our results we add a PatternTransformer to the Composite Web Part. In the RowTemplate we can access the conversion results via the expression variable DataItem:

<h1>
  1 {ConnectionData.From.First().Key} 
  = 
  {DataItem} {ConnectionData.To.First().Key}
</h1>

This is what it looks in the end:

what it looks like

Comments
ABOUT

This blog is about technical and non-technical aspects of the product MatchPoint and other SharePoint topics.

If you would like to post an article or if you have an idea for a post, please contact us.

ARCHIVE
COMMENTS
Reto Jeger
04.10.2017 09:15
Hello Reiner,
Thanks for pointing out the missing ... | Goto Post
rganser
29.09.2017 09:56
Hi, I downloaded the ZIP-file for MatchPoint Versi... | Goto Post
Glenn De Block
26.07.2017 03:54
Oh, I see creator now uses an EvaluateAction.
Nice... | Goto Post
Glenn De Block
26.07.2017 02:41
We recently upgraded our version, are you guys sur... | Goto Post
Sandra Perz
09.09.2016 12:22
Thanks, that helps a lot. | Goto Post