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.
- Open the configuration and select the node Fields.
- Add a ChoiceField and set the Name to "From".
- Add a SPListChoiceProvider and set the ListUrl to the currencies list.
- Set the KeyColumn to the column on the list where the currency code is stored.
- Set the TextColumn to a descriptive column if available. Otherwise just use the same as in the
KeyColumn.
- 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:
