SilverlightIntrawebPart2


We created our Intraweb REST Server in part one. Now we need to consume the data from Silverlight. To do so, create a new Silverlight application (don't need to create a Web Application also, since the control will be hosted in Intraweb. Just create a test page) and add the following code to the Page.xaml

<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="Silverlight.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="530">
    <Grid x:Name="LayoutRoot" Background="White">
      <Canvas>
        <TextBox x:Name="textFirstName" Width="200" />
        <Button x:Name="butnSearch" Width="100" Content="Search" Canvas.Left="205" Click="SearchFirstName"/>
        <TextBox x:Name="textResult" Height="250" Width="400" Canvas.Top="30"/>
      </Canvas> 
    </Grid>
</UserControl>

I'm using a TextBox for the result because the DataGrid because I want to show some further processing. With the DataGrid, in principal it would be sufficient to assign the result of the query to the ItemsSource property and you would have your data available in the grid. Another reason is because every time I tried to do some simple stuff with the DataGrid, it kept crashing Visual Studio.

The Search Click handler should have the following code:


    private void SearchFirstName(object sender, RoutedEventArgs e) {
      var client = new WebClient();
      client.DownloadStringCompleted += client_DownloadStringCompleted;
      client.DownloadStringAsync(
        new Uri(String.Format("http://127.0.0.1:8888/services/customerData?search={0}", textFirstName.Text)));
    }

We are using a WebClient to make a REST request. So as not to block the page, we are performing an asynchronous request. It is important to note that by default you cannot make cross-domain calls from Silverlight (for security reasons). In our case it's not an issue since we are going to host the control in our Intraweb application and our data is obtained from the same source.

Once the call has been completed and our client_DownloadStringCompleted is called, we can use LINQ to XML to process the information.

    private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
      var customerList = XDocument.Parse(e.Result);

      var customers = from customer in customerList.Descendants("Customer")
                      select new Customer
                      {
                        FirstName = (string)customer.Element("FirstName"),
                        LastName = (string)customer.Element("LastName"),
                        EmailAddress = (string)customer.Element("EmailAddress")
                      };
      foreach(var customer in customers) {
        if (customer.EmailAddress.Length > 0 ) {
          textResult.Text = textResult.Text +
                            String.Format("{0} {1}: {2}\n", customer.FirstName, customer.LastName, customer.EmailAddress);
        }
      }
    }


We are using Linq to XML to process the result obtained from Intraweb and based on some useless condition, I add information to the text box.

To host the control inside of Intraweb, we drop a TIWSilverlight control on the form and point the XAP.Url property to '/files/Silverlight.xap'. This is the packaged file result of building the Silverlight control inside of Visual Studio and placed in the files directory of Intraweb (you can point Visual Studio to output there during development).

So far we've seen how to obtain data from Intraweb and consume this data in Silverlight. Next we'll see how to update an Intraweb control using Silverlight.