In Smart Office 10.2.1.0.367 HF38 there is support for JSON when calling a REST data service from a Mashup. In this blog post I’m going to use a fake online REST service to demonstrate how it’s done.
The response data from the REST service call can be converted into a DataItem hierarchy or as a JSON string. This example will fetch a listing of users from the service and present them in a Mashup ListView and in a TextBox as a JSON string, side-by-side.
The Mashup XAML syntax will be the same as before when doing a REST service call. All you need to do now is set the REST.AcceptHeader parameter to application/json.
The REST.OutputType determines if the data response is available to the Mashup as an object hierarchy or as a JSON string. Use the value ‘DataItem’ to get DataItem hierarchy or set ‘application/json’ for a JSON string. If REST.OutputType is not set the default will be a DataItem hierarchy.
<mashup:DataListPanel.DataService> <mashup:DataService Type="REST"> <mashup:DataService.Operations> <mashup:DataOperation Name="List"> <mashup:DataParameter Key="REST.BaseAddress" Value="https://jsonplaceholder.typicode.com/users/" /> <mashup:DataParameter Key="REST.OutputType" Value="DataItem" /> <mashup:DataParameter Key="REST.AcceptHeader" Value="application/json" /> </mashup:DataOperation> </mashup:DataService.Operations> </mashup:DataService> </mashup:DataListPanel.DataService>
To set the first column in the ListView to the company name use the binding below.
DisplayMemberBinding="{Binding [company].[name]}"
To access the JSON string when that is set as output type simply bind to ‘json’.
Text="{Binding [json]}"
Here is the complete Mashup code if you would like to try it out.
<Grid Margin="15,10,15,15" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="clr-namespace:Mango.UI.Controls;assembly=Mango.UI" xmlns:mashup="clr-namespace:Mango.UI.Services.Mashup;assembly=Mango.UI" xmlns:m3="clr-namespace:MForms.Mashup;assembly=MForms" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Grid.RowDefinitions> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="1*" /> </Grid.ColumnDefinitions> <mashup:DataListPanel DockPanel.Dock="Top" Name="UsersList" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3"> <mashup:DataListPanel.DataService> <mashup:DataService Type="REST"> <mashup:DataService.Operations> <mashup:DataOperation Name="List"> <mashup:DataParameter Key="REST.BaseAddress" Value="https://jsonplaceholder.typicode.com/users/" /> <mashup:DataParameter Key="REST.OutputType" Value="DataItem" /> <mashup:DataParameter Key="REST.AcceptHeader" Value="application/json" /> </mashup:DataOperation> </mashup:DataService.Operations> </mashup:DataService> </mashup:DataListPanel.DataService> <mashup:DataListPanel.Events> <mashup:Events> <mashup:Event SourceEventName="Startup" TargetEventName="List" /> </mashup:Events> </mashup:DataListPanel.Events> <DockPanel LastChildFill="False"> <ListView DockPanel.Dock="Top" Name="ListViewCompany" ItemsSource="{Binding Items}" Style="{DynamicResource styleListView}" ItemContainerStyle="{DynamicResource styleListViewItem}"> <ListView.View> <GridView ColumnHeaderContainerStyle="{DynamicResource styleGridViewColumnHeader}"> <GridView.Columns> <GridViewColumn Header="Company" DisplayMemberBinding="{Binding [company].[name]}" /> <GridViewColumn Header="City" DisplayMemberBinding="{Binding [address].[city]}" /> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding [name]}" /> <GridViewColumn Header="Email" DisplayMemberBinding="{Binding [email]}" /> <GridViewColumn Header="Longitude" DisplayMemberBinding="{Binding [address].[geo].[lng]}" /> <GridViewColumn Header="Longitude" DisplayMemberBinding="{Binding [address].[geo].[lng]}" /> </GridView.Columns> </GridView> </ListView.View> </ListView> </DockPanel> </mashup:DataListPanel> <mashup:DataPanel Grid.Row="0" Grid.Column="2" Name="JsonString"> <mashup:DataPanel.DataService> <mashup:DataService Type="REST"> <mashup:DataService.Operations> <mashup:DataOperation Name="List"> <mashup:DataParameter Key="REST.BaseAddress" Value="https://jsonplaceholder.typicode.com/users/" /> <mashup:DataParameter Key="REST.AcceptHeader" Value="application/json" /> <mashup:DataParameter Key="REST.OutputType" Value="application/json" /> </mashup:DataOperation> </mashup:DataService.Operations> </mashup:DataService> </mashup:DataPanel.DataService> <mashup:DataPanel.Events> <mashup:Events> <mashup:Event SourceEventName="Startup" TargetEventName="List" /> </mashup:Events> </mashup:DataPanel.Events> <Grid Margin="10,0,10,0"> <TextBox Text="{Binding [json]}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AcceptsReturn="True" VerticalContentAlignment="Top" TextWrapping="Wrap" /> </Grid> </mashup:DataPanel> </Grid>
Great example, but how would the code be, if you would have to send also username and password – authentification basic?? Thx.
This is covered in other posts. Basically it’s a replacement variable that you will use.