So far in this series, we have done the following…
Part 1 > Setup database, create entity data model, create a simple domain service, and expose OData end points
Part 2 > Consume OData Endpoints using Internet Explorer [or any other client] for that matter
In this post, initially you will learn how to create new methods in the Domain Service, and consume it in Silverlight using Code.
Open dsAlbumAndArtist.cs and add a new method called GetSortedAlbums as follows…

Add a new page to the ChinookSample -> Views Folder…

Inside your MainPage.xaml, add the highlighted code so that you could navigate to page Albums…
<Border x:Name="LinksBorder" Style="{StaticResource LinksBorderStyle}">
<StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}">
<HyperlinkButton x:Name="Link1" Style="{StaticResource LinkStyle}"
NavigateUri="/Home" TargetName="ContentFrame"
Content="{Binding Path=ApplicationStrings.HomePageTitle,
Source={StaticResource ResourceWrapper}}"/>
<Rectangle x:Name="Divider1" Style="{StaticResource DividerStyle}"/>
<HyperlinkButton x:Name="Link2" Style="{StaticResource LinkStyle}"
NavigateUri="/About" TargetName="ContentFrame"
Content="{Binding Path=ApplicationStrings.AboutPageTitle,
Source={StaticResource ResourceWrapper}}"/>
<Rectangle x:Name="Divider2" Style="{StaticResource DividerStyle}"/>
<HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}"
NavigateUri="/Albums" TargetName="ContentFrame" Content="Albums"/>
</StackPanel>
</Border>
In the Albums.xaml page, Inside the <Grid x:Name="LayoutRoot"> add the following code…
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Albums" Name="btnAlbums" Margin="10" Grid.Column="0"/>
<Button Content="Sorted Albums" Name="btnSortedAlbums" Margin="10" Grid.Column="1" />
<sdk:DataGrid Grid.Row="1" Grid.Column="0" Margin="10" Name="gridAlbums">
</sdk:DataGrid>
<sdk:DataGrid Grid.Row="1" Grid.Column="1" Margin="10" Name="gridAlbumsSorted">
</sdk:DataGrid>
Finally, your code behind for Albums.xaml.cs should like the following…
Notice, that using ChinookSample.Web.Services is added in the Silverlight application and enabled Silverlight to talk to the RIA Services automatically using the context.
Also notice that calling a method/service is really very simple and can be accomplished in hardly three lines of code. All the code plumbing is already done for you by RIA services in the background.
using System.Windows;
using System.Windows.Controls;
using ChinookSample.Web.Services;
namespace ChinookSample.Views
{
public partial class Albums : Page
{
public Albums()
{
InitializeComponent();
btnAlbums.Click += new RoutedEventHandler(btnAlbums_Click);
btnSortedAlbums.Click += new RoutedEventHandler(btnSortedAlbums_Click);
}
void btnSortedAlbums_Click(object sender, RoutedEventArgs e)
{
dsAlbumAndArtist context = new dsAlbumAndArtist();
gridAlbumsSorted.ItemsSource = context.Albums;
context.Load(context.GetSortedAlbumsQuery());
}
void btnAlbums_Click(object sender, RoutedEventArgs e)
{
dsAlbumAndArtist context = new dsAlbumAndArtist();
gridAlbums.ItemsSource = context.Albums;
context.Load(context.GetAlbumsQuery());
}
}
}
Once you run this application, you will see an output as follows…
In the next post, we'll discuss how to achieve the same effect declaratively using XAML. Stay tuned.
Until next time,
Rahul
Quote of the day:
Blame someone else and get on with your life. - Alan Woods