<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>SharePoint Magazine &#187; linq</title>
	<atom:link href="http://sharepointmagazine.net/tag/linq/feed" rel="self" type="application/rss+xml" />
	<link>http://sharepointmagazine.net</link>
	<description>SharePoint Magazine is an online Magazine dedicated to the world of SharePoint</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:14:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Creating a SharePoint Web Part that hosts a data binding Silverlight application</title>
		<link>http://sharepointmagazine.net/technical/development/creating-a-sharepoint-web-part-that-hosts-a-data-binding-silverlight-application</link>
		<comments>http://sharepointmagazine.net/technical/development/creating-a-sharepoint-web-part-that-hosts-a-data-binding-silverlight-application#comments</comments>
		<pubDate>Tue, 11 Nov 2008 10:25:46 +0000</pubDate>
		<dc:creator>Karine Bosch</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[blueprint]]></category>
		<category><![CDATA[CreateChildControls]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[sharepoint]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[UpdateListItems]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://sharepointmagazine.net/?p=1106</guid>
		<description><![CDATA[In the beginning of this year I started working on the Silverlight BluePrint for SharePoint together with my mentor Patrick Tisseghem who suddenly passed away. I dedicate this article to him.]]></description>
			<content:encoded><![CDATA[<div class="mceTemp mceIEcenter">In the beginning of this year I started working on the <strong>Silverlight BluePrint for SharePoint</strong> together with my mentor Patrick Tisseghem who suddenly passed away recently. I dedicate this article to him.</div>
<p>With Silverlight you can really light up your SharePoint user interfaces. It can be used in different SharePoint domains like web parts, application pages, navigation, content management, custom fields, editor parts, etc.</p>
<p>In this tutorial I&#8217;m going to explain how you can host a <strong>Silverlight 2 beta 2</strong> application from within a SharePoint Web Part. The Web Part will pass the URL of the SharePoint site together with the name of the list for which the Silverlight application will show the data. The retrieval of the data will be done by the Silverlight application using the <strong>HttpWebRequest</strong> technique for calling the <strong>SharePoint web services</strong>. As the SharePoint web services return a chunk of XML the XML will be handled by using <strong>LINQ for XML</strong>. The data will be bound to the Silverlight controls.</p>
<p>When the Web Part loads the complete list of AdventureWorks products will be loaded. In the Topic box you can enter a part of a product number and click the Search button. Based on that search string a restricted list of products will be returned. You can select  a product from the list to view its details.</p>
<div id="attachment_1126" class="wp-caption aligncenter" style="width: 510px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-listbox3.png"><img class="size-full wp-image-1126" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-listbox3.png" alt="AdventureWorks listbox" width="500" height="450" /></a><p class="wp-caption-text">AdventureWorks listbox</p></div>
<p>You can also modify its list price if you want and update the AdventureWorks products list. This will again use the <strong>HttpWebRequest</strong> technique to call the <strong>UpdateListItems</strong> method of the Lists.asmx web service.</p>
<div id="attachment_1127" class="wp-caption aligncenter" style="width: 510px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-details2.png"><img class="size-full wp-image-1127" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-details2.png" alt="AdventureWorks product details control" width="500" height="209" /></a><p class="wp-caption-text">AdventureWorks product details control</p></div>
<p>The Silverlight application will be deployed as an embedded resource of the SharePoint Web Part.</p>
<p>You can download the source code of this tutorial from here. The zip contains the sources for the SharePoint Web Part and the Silverlight application, together with two SharePoint list templates for the AdventureWorks Products and the AdventureWorks Product Pictures.</p>
<h2>Creating the Web Part</h2>
<p>I created the sample Web Part using the <strong>Visual Studio 2008 extenstions for WSS 3.0</strong>. It creates the necessary infrastructure for the deployment afterwards.</p>
<div id="attachment_1128" class="wp-caption aligncenter" style="width: 271px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-web-part-project2.png"><img class="size-full wp-image-1128" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/adventureworks-web-part-project2.png" alt="AdventureWorks Web Part project" width="261" height="114" /></a><p class="wp-caption-text">AdventureWorks Web Part project</p></div>
<p>Before you can start coding you need to add a reference to the System.Web.Silverlight.dll (version 2.0.5.0) and to the System.Web.Extensions.dll (version 3.5.0.0).</p>
<p>Open the <strong>AdventureWorksProducts.cs</strong> file in Code View and declare a class level variable for the silverlight control that will host the silverlight application:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span></span>.<span style="color: #0000FF;">SilverlightControls</span>.<span style="color: #0000FF;">Silverlight</span> silverlightControl <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span></pre></div></div>

<p>Override the <strong>OnLoad</strong> method in which you need to check whether a script manager already exists on the page. If not, you have to add one at the first position of the <strong>Controls</strong> collection:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnLoad</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// Script manager instance may appear only once on a page</span>
      ScriptManager scriptManager <span style="color: #008000;">=</span> ScriptManager.<span style="color: #0000FF;">GetCurrent</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Page</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>scriptManager <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          scriptManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ScriptManager<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">AddAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, scriptManager<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Within the <strong>CreateChildControls</strong> method that is already prepared for you, you have to instantiate the Silverlight control and set its properties. One of these properties is the <strong>Source</strong> property. The setting depends on where you are going to deploy your silverlight application. In this sample I will deploy the Silverlight application as an embedded resource of my SharePoint Web Part so the <strong>Source</strong> property has to point to that location. I will come back to this property once the Silverlight application is created. Don&#8217;t forget to specify a <strong>Width</strong> and <strong>Height</strong>, otherwise the Silverlight application will not be visible. Another property that is worth mentioning is the <strong>InitParameters</strong> property. I use this property to pass the URL to the SharePoint site and the name from the list that needs to be queried. The retrieval of the data, and thus the communication with SharePoint, will be done from within the Silverlight application. Notice that this property is a string. You can pass data in it by respecting the syntax <strong>name1=value1,name2=value2,&#8230;</strong> Within the Silverlight application this data will be unpacked in a dictionary. But more on this in next section.</p>
<p>Don&#8217;t forget to add the Silverlight control to the Controls collection of the Web Part.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">CreateChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// instantiation of the silverlight control</span>
      silverlightControl <span style="color: #008000;">=</span>
            <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span></span>.<span style="color: #0000FF;">SilverlightControls</span>.<span style="color: #0000FF;">Silverlight</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      silverlightControl.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;SLAdventureWorks&quot;</span><span style="color: #008000;">;</span>
      silverlightControl.<span style="color: #0000FF;">MinimumVersion</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2.0.30523&quot;</span><span style="color: #008000;">;</span>
      silverlightControl.<span style="color: #0000FF;">Width</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">750</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      silverlightControl.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">600</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      silverlightControl.<span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// parameters passed are the the URL of the SharePoint site and the</span>
      <span style="color: #008080; font-style: italic;">// name of the list that contains the AdventureWorks products</span>
      silverlightControl.<span style="color: #0000FF;">InitParameters</span> <span style="color: #008000;">=</span>
         <span style="color: #666666;">&quot;siteurl=&quot;</span> <span style="color: #008000;">+</span> SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Url</span>
          <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;,listname=AdventureWorks Products&quot;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>silverlightControl<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now override the <strong>RenderContents</strong> method to render the Silverlight control:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RenderContents<span style="color: #000000;">&#40;</span>HtmlTextWriter writer<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>silverlightControl <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
         silverlightControl.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>That&#8217;s all for the Web Part code.</p>
<h2>Creating the Silverlight application</h2>
<p>The development of the Silverlight application is a bit harder, certainly if you are a normal ASP.NET or SharePoint developer. Add a new project to the solution using the <strong>Silverlight Application</strong> template. This template comes with a standard <strong>Page.xaml</strong> and an <strong>App.xaml</strong>. The <strong>App.xaml</strong> file inherits from the <strong>System.Windows.Application</strong> class which represents the Silverlight application while the <strong>Page</strong> represents a Silverlight control.</p>
<p>Before designing the Silverlight controls you will have to retrieve the parameters that have been passed by the SharePoint Web Part. Open the <strong>App.xaml.cs</strong> file and locate the <strong>Application_Startup</strong> method. The second incoming argument is of type <strong>StartUpEventArgs</strong> and contains in its <strong>InitParams</strong> property the data you passed in via the Web Part. At this side of the Silverlight application it is a dictionary of key-value pairs.</p>
<p>To be able to use this data from within the Silverlight application you have to pass the data to constructor of the Page control.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Application_Startup<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, StartupEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #FF0000;">string</span> siteUrl <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
     <span style="color: #FF0000;">string</span> listName <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">InitParams</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> e.<span style="color: #0000FF;">InitParams</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
         siteUrl <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">InitParams</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;siteurl&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
         listName <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">InitParams</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;listname&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// Load the main control</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RootVisual</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Page<span style="color: #000000;">&#40;</span>siteUrl, listName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Open the Page.xaml.cs code behind file and modify the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> Page<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> siteUrl, <span style="color: #FF0000;">string</span> listName<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The Silverlight application also contains a class that defines the Product object. The different text boxes in the Silverlight controls will be bound to the properties of this type of objects. The class inherits from <strong>INotifyPropertyChanged</strong>, to enable the object to be notified when the data changes. This interface requires only one thing: that the class has an event of type <strong>PropertyChangedEventHandler</strong>, named <strong>PropertyChanged</strong>.  This event is fired when any property that is tied to a UI control is changed. In this case the Product object will be notified when the user tries to update the product price.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Product <span style="color: #008000;">:</span> INotifyPropertyChanged
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ProductId <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ProductNumber <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ProductName <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Description <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">double</span> listPrice<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> ListPrice <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> listPrice<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
           set <span style="color: #000000;">&#123;</span> listPrice <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> OnPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ListPrice&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Color <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Size <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Weight <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ThumbNail <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region INotifyPropertyChanged Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> OnPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>PropertyChanged <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                PropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #000000;">&#40;</span>name<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now it is time to design the Silverlight controls.</p>
<p>The Silverlight application consists of 3 controls: the main control that displays a search box with a button and a ListBox for displaying the list of AdventureWorks products. The second control displays the details of the product selected from the list. In the details control you will also be able to update the product price and save your changes to the SharePoint site. The third control is a rotator control that is only shown when data is retrieved from the SharePoint web service with the purpose to show the user that the application is busy.</p>
<p>The XAML for the main Silverlight control is not much:</p>
<pre>&lt;UserControl x:Class="SL.XAML.AdventureWorksProducts.Page"</pre>
<pre>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</pre>
<pre>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"</pre>
<pre>    xmlns:my="clr-namespace:SL.XAML.AdventureWorksProducts"</pre>
<pre>    Width="750" Height="600"&gt;</pre>
<pre>    &lt;Grid x:Name="LayoutRoot" Background="White"&gt;</pre>
<pre>        &lt;Grid.RowDefinitions&gt;</pre>
<pre>            &lt;RowDefinition Height="40"/&gt;</pre>
<pre>            &lt;RowDefinition Height="*"/&gt;</pre>
<pre>        &lt;/Grid.RowDefinitions&gt;</pre>
<pre>        &lt;Grid Style="{StaticResource Header}"&gt;</pre>
<pre>            &lt;Grid.ColumnDefinitions&gt;</pre>
<pre>                &lt;ColumnDefinition Width="*"/&gt;</pre>
<pre>                &lt;ColumnDefinition Width="250"/&gt;</pre>
<pre>                &lt;ColumnDefinition Width="80"/&gt;</pre>
<pre>            &lt;/Grid.ColumnDefinitions&gt;</pre>
<pre>            &lt;Border Style="{StaticResource TitleBorder}"&gt;</pre>
<pre>                &lt;TextBlock Text="AdventureWorks Product Search"</pre>
<pre>                           Style="{StaticResource TitleText}"  /&gt;</pre>
<pre>            &lt;/Border&gt;</pre>
<pre>            &lt;TextBox x:Name="SearchTextBox" Text="Topic..."</pre>
<pre>                     FontFamily="Trebuchet MS" Grid.Column="1"</pre>
<pre>                     FontSize="12" Padding="1,3,1,1"/&gt;</pre>
<pre>            &lt;Button x:Name="SearchButton"</pre>
<pre>                    Content="Search"</pre>
<pre>                    Click="SearchButton_Click"</pre>
<pre>                    Style="{StaticResource SearchButton}" /&gt;</pre>
<pre>        &lt;/Grid&gt;</pre>
<pre>        &lt;ListBox x:Name="ProductsList" Style="{StaticResource ProductsList}"</pre>
<pre>                 SelectionChanged="ProductsList_SelectionChanged"&gt;</pre>
<pre>            &lt;ListBox.ItemTemplate&gt;</pre>
<pre>                &lt;DataTemplate&gt;</pre>
<pre>                    &lt;StackPanel Orientation="Horizontal"&gt;</pre>
<pre>                        &lt;!-- Product Thumbnail --&gt;</pre>
<pre>                        &lt;Image Source="{Binding ThumbNail}"</pre>
<pre>                               Style="{StaticResource ThumbNailPreview}" /&gt;</pre>
<pre>                        &lt;!-- Product Number--&gt;</pre>
<pre>                        &lt;StackPanel Orientation="Vertical"&gt;</pre>
<pre>                            &lt;TextBlock Text="{Binding ProductNumber}"</pre>
<pre>                               Margin="5"</pre>
<pre>                               Style="{StaticResource TitleBlock}"</pre>
<pre>                               FontWeight="Bold" /&gt;</pre>
<pre>                            &lt;TextBlock Text="{Binding ProductName}"</pre>
<pre>                               Margin="5"</pre>
<pre>                               Style="{StaticResource TitleBlock}" /&gt;</pre>
<pre>                        &lt;/StackPanel&gt;</pre>
<pre>                        &lt;!-- List Price--&gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding ListPrice, Mode=TwoWay}"</pre>
<pre>                               Margin="5"</pre>
<pre>                               Style="{StaticResource PriceBlock}"/&gt;</pre>
<pre>                    &lt;/StackPanel&gt;</pre>
<pre>                &lt;/DataTemplate&gt;</pre>
<pre>            &lt;/ListBox.ItemTemplate&gt;</pre>
<pre>        &lt;/ListBox&gt;</pre>
<pre>        &lt;TextBlock x:Name="MessageTextBlock" Grid.RowSpan="2"</pre>
<pre>                   Visibility="Collapsed" Text="No items found" /&gt;</pre>
<pre>        &lt;my:ProductDetails x:Name="DetailsView" Grid.RowSpan="2"</pre>
<pre>                    Visibility="Collapsed" /&gt;</pre>
<pre>        &lt;my:Rotator x:Name="LoadRotator" Grid.RowSpan="2"</pre>
<pre>                    Visibility="Collapsed" /&gt;</pre>
<pre>    &lt;/Grid&gt;</pre>
<pre>&lt;/UserControl&gt;</pre>
<p>The main grid contains two rows: one for the search text box and button, and one for the products list box.</p>
<p>The first row of the main grid contains a second grid with a border and 3 columns: one for the Title, one for the search text and one for the search button. It applies a style that is defined as resource in the <strong>App.xaml</strong> file defining the margin:</p>
<pre>&lt;Style x:Key="Header" TargetType="Grid"&gt;</pre>
<pre>       &lt;Setter Property="Margin" Value="7" /&gt;</pre>
<pre>       &lt;Setter Property="Grid.Row" Value="0"/&gt;</pre>
<pre>&lt;/Style&gt;</pre>
<p>Also the border, the title and the button apply a style that is defined as resource in the App.xaml. The button has an event handler attached to it, which will be explained later in the article.</p>
<p>The second row of the main grid contains the list box for the AdventureWorks products. The list box contains a number of stack panels for the layout of the product data. Besides styles the individual text boxes also bind to properties of the product object. For the list price text block, the binding mode is set to <strong>TwoWay</strong>.</p>
<p>At the end of the Page control you see the XAML for the <strong>ProductDetails</strong> control. Initially this control is hidden. It is only when the user selects a product that this controls is populated with the selected product and that the controls is made visible.</p>
<p>The rotator control will only be shown when data is retrieved from the SharePoint web service.</p>
<p>The <strong>ProductDetails</strong> control contains a main grid that contains a rectangle and border element defining the background. The controls displaying the details of the selected product are drawn within grids and stack panels for a good positioning on the control. Most of the controls apply styles defined in the App.xaml. The text blocks displaying the data are bound to a property of the product object. The XAML for the control looks as follows:</p>
<pre>&lt;UserControl x:Class="SL.XAML.AdventureWorksProducts.ProductDetails"</pre>
<pre>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</pre>
<pre>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"</pre>
<pre>    Width="600" Height="250"&gt;</pre>
<pre>    &lt;Grid&gt;</pre>
<pre>        &lt;Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch"</pre>
<pre>              Opacity="0.765" Fill="#FF8A8A8A" /&gt;</pre>
<pre>         &lt;Border CornerRadius="20" Background="#FF5C7590" Width="600" Height="250"&gt;</pre>
<pre>             &lt;StackPanel Margin="5, 7, 0, 5"&gt;</pre>
<pre>                &lt;!-- Top Right Close Button --&gt;</pre>
<pre>                &lt;Button Content="Close" Style="{StaticResource CloseButton}" Click="Button_Click"/&gt;</pre>
<pre>                 &lt;Grid&gt;</pre>
<pre>                    &lt;Grid.RowDefinitions&gt;</pre>
<pre>                        &lt;RowDefinition Height="150"/&gt;</pre>
<pre>                        &lt;RowDefinition Height="*"/&gt;</pre>
<pre>                    &lt;/Grid.RowDefinitions&gt;</pre>
<pre>                     &lt;Grid.ColumnDefinitions&gt;</pre>
<pre>                        &lt;ColumnDefinition Width="180"/&gt;</pre>
<pre>                        &lt;ColumnDefinition Width="150" /&gt;</pre>
<pre>                        &lt;ColumnDefinition Width="*" /&gt;</pre>
<pre>                    &lt;/Grid.ColumnDefinitions&gt;</pre>
<pre>                     &lt;!-- Left: Large Picture --&gt;</pre>
<pre>                    &lt;Image Source="{Binding ThumbNail}" Style="{StaticResource DetailsThumbNailPreview}"/&gt;</pre>
<pre>                     &lt;!-- Center: Product Details --&gt;</pre>
<pre>                    &lt;StackPanel Orientation="Vertical" Grid.Column="1" &gt;</pre>
<pre>                        &lt;TextBlock Text="Product number:" Style="{StaticResource DetailBlock}" FontWeight="Bold"  Height="30"/&gt;</pre>
<pre>                        &lt;TextBlock Text="Product name:" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="Color:" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="Size:" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="Weight:" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="List price in $:" Style="{StaticResource DetailBlock}" Height="30" /&gt;</pre>
<pre>                        &lt;TextBlock Text="Description:" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                    &lt;/StackPanel&gt;</pre>
<pre>                    &lt;StackPanel Orientation="Vertical" Grid.Column="2" &gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding ProductNumber}" Style="{StaticResource DetailBlock}" FontWeight="Bold" Height="30"/&gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding ProductName}" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding Color}" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding Size}" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock Text="{Binding Weight}" Style="{StaticResource DetailBlock}" /&gt;</pre>
<pre>                        &lt;TextBlock x:Name="ListPriceTextBlock" Text="{Binding ListPrice, Mode=TwoWay }" Style="{StaticResource DetailBlock}" Height="30" /&gt;</pre>
<pre>                        &lt;TextBox x:Name="ListPriceTextBox" Text="" Width="200" Visibility="Collapsed" HorizontalAlignment="Left" /&gt;</pre>
<pre>                    &lt;/StackPanel&gt;</pre>
<pre>                     &lt;TextBlock Text="{Binding Description}" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"</pre>
<pre>                         Style="{StaticResource DetailBlock}" FontStyle="Italic" /&gt;</pre>
<pre>                &lt;/Grid&gt;</pre>
<pre>                 &lt;Button x:Name="EditButton" Width="80" Content="Edit Price" HorizontalAlignment="Right" Margin="20" Click="EditButton_Click"/&gt;</pre>
<pre>             &lt;/StackPanel&gt;</pre>
<pre>         &lt;/Border&gt;</pre>
<pre>    &lt;/Grid&gt;</pre>
<pre>&lt;/UserControl&gt;</pre>
<p>Now to the code behind of both Silverlight controls. When the Silverlight application loads, all products of the AdventureWorks Products list are retrieved using the <strong>HttpWebRequest</strong> for calling the <strong>GetListItems</strong> method of the <strong>Lists.asmx</strong> web service.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #000000;">&#40;</span>siteUrl <span style="color: #008000;">+</span>
     <span style="color: #666666;">&quot;/_vti_bin/Lists.asmx&quot;</span>, UriKind.<span style="color: #0000FF;">Absolute</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
request.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span>
request.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/soap+xml; charset=utf-8&quot;</span><span style="color: #008000;">;</span>
request.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ClientType&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Silverlight&quot;</span><span style="color: #008000;">;</span>
request.<span style="color: #0000FF;">BeginGetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>RequestCallback<span style="color: #000000;">&#41;</span>, request<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The call to the Request is asynchronous an thus needs a callback method. In that callback method the soap envelope is build. If you don&#8217;t know the correct envelope of a web service method you have to browse to that web service and check the method of your choice. You will get a page with the necessary information of the envelope.</p>
<p>The envelope contains the necessary XML to execute the <strong>GetListItems</strong> method on the AdventureWorks Products list passed in via the Web Part. If the user entered a string in the Search text box a CAML query is build to retrieve only the products containing that string.  The body of the request is retrieved by calling the <strong>EndGetRequestStream</strong> method of the <strong>HttpWebRequest</strong> object. Then the envelope is written to the body and the request is send back to the server.</p>
<p>Also the <strong>Response</strong> is asynchronous so you need to define another callback method for <strong>HttpWebRequest</strong> when coming back from the server.</p>
<p>The envelope is constructed as follows:</p>
<div id="attachment_1159" class="wp-caption aligncenter" style="width: 604px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/getlistitems-envelope.png"><img class="size-full wp-image-1159" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/getlistitems-envelope.png" alt="GetListItems envelope" width="594" height="337" /></a><p class="wp-caption-text">GetListItems envelope</p></div>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> RequestCallback<span style="color: #000000;">&#40;</span>IAsyncResult asyncResult<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">try</span>
     <span style="color: #000000;">&#123;</span>
         <span style="color: #FF0000;">string</span> envelope <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;see image&quot;</span><span style="color: #008000;">;</span>
&nbsp;
         <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
         <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>searchstring<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> searchstring <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;Topic...&quot;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
             query <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;{0}&quot;</span><span style="color: #008000;">;</span>
             query <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>query, searchstring<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         <span style="color: #000000;">&#125;</span>
         envelope <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>envelope, listName, query<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>asyncResult.<span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
         body <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">EndGetRequestStream</span><span style="color: #000000;">&#40;</span>asyncResult<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span> encoding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> formBytes <span style="color: #008000;">=</span> encoding.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>envelope<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         body.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>formBytes, <span style="color: #FF0000;">0</span>, formBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         body.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         request.<span style="color: #0000FF;">BeginGetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>ResponseCallback<span style="color: #000000;">&#41;</span>,
            request<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>WebException ex<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
         <span style="color: #FF0000;">string</span> s <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Both the Request and the Response are started asynchronously on another thread. When the response comes back from the server, it is not on the worker thread. So the data is stored in a class level variable and the <strong>ProcessResponse</strong> method on the worker thread is called.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ResponseCallback<span style="color: #000000;">&#40;</span>IAsyncResult asyncResult<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     LoadRotator.<span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility.<span style="color: #0000FF;">Collapsed</span><span style="color: #008000;">;</span>
&nbsp;
     HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>asyncResult.<span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
     HttpWebResponse response <span style="color: #008000;">=</span>
        <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">EndGetResponse</span><span style="color: #000000;">&#40;</span>asyncResult<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">==</span> HttpStatusCode.<span style="color: #0000FF;">OK</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
        products <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Stream content <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            responsestring <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Dispatcher</span>.<span style="color: #0000FF;">BeginInvoke</span><span style="color: #000000;">&#40;</span>ProcessResponse<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">else</span>
     <span style="color: #000000;">&#123;</span>
         MessageTextBlock.<span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility.<span style="color: #0000FF;">Visible</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The <strong>ProcessResponse</strong> method parses the XML into a collection of Product objects using <strong>LINQ to XML</strong>. For this purpose a reference to the <strong>System.Xml.Linq</strong> assembly is added. All products are added to the Products collection and then assigned to the <strong>ItemSource</strong> property of the products list box in order to enable the data binding.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ProcessResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    LoadRotator.<span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility.<span style="color: #0000FF;">Collapsed</span><span style="color: #008000;">;</span>
    products <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    XDocument results <span style="color: #008000;">=</span> XDocument.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>responsestring<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var query <span style="color: #008000;">=</span> from item <span style="color: #0600FF;">in</span> results.<span style="color: #0000FF;">Descendants</span><span style="color: #000000;">&#40;</span>XName.<span style="color: #0000FF;">Get</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;row&quot;</span>,
        <span style="color: #666666;">&quot;#RowsetSchema&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    select <span style="color: #008000;">new</span> Product<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Id <span style="color: #008000;">=</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Convert</span>.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_ID&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span>,
            CultureInfo.<span style="color: #0000FF;">InvariantCulture</span><span style="color: #000000;">&#41;</span>,
        ProductNumber <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Title&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;_&quot;</span>,
        ProductName <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_ProductName&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span>,
        ListPrice <span style="color: #008000;">=</span>
            <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Convert</span>.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_ListPrice&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span>,
            CultureInfo.<span style="color: #0000FF;">InvariantCulture</span><span style="color: #000000;">&#41;</span>,
        Description <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Description&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span>
            item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Description&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>,
        Color <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Color&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span>
            item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Color&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>,
        Size <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Size&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span>
            item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Size&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>,
        Weight <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Weight&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span>
            item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Weight&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>,
        ThumbNail <span style="color: #008000;">=</span> GetThumbnailUrl<span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ows_Thumbnail&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Product p <span style="color: #0600FF;">in</span> query.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        products.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    ProductsList.<span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> products<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When the user selects a product in the list, a box containing the detailed information is shown to the user. This is triggered by the <strong>SelectionChanged</strong> event of the list box. The selected product is retrieved from the list box and assigned to the <strong>ProductDetails</strong> control, which is then made visible.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ProductsList_SelectionChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender,
     SelectionChangedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     Product product <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Product<span style="color: #000000;">&#41;</span>ProductsList.<span style="color: #0000FF;">SelectedItem</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>product <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
         DetailsView.<span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> product<span style="color: #008000;">;</span>
         DetailsView.<span style="color: #0000FF;">SiteUrl</span> <span style="color: #008000;">=</span> siteUrl<span style="color: #008000;">;</span>
         DetailsView.<span style="color: #0000FF;">ListName</span> <span style="color: #008000;">=</span> listName<span style="color: #008000;">;</span>
         DetailsView.<span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility.<span style="color: #0000FF;">Visible</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This brings us to the code behind of the <strong>ProductDetails</strong> control. The control is populated by the main control. This control contains also an Edit Price button. When the user clicks this button, the list price text block is changed into a text box in which the user can enter the new price. When the user then clicks the <strong>Update</strong> button,  the <strong>UpdateListItems</strong> method of the Lists.asmx web service is called to save the new list price. The update is also performed by using HttpWebRequest, which runs asynchronously on different threads.</p>
<p>The envelope for the UpdateListItems method looks like follows:</p>
<div id="attachment_1162" class="wp-caption aligncenter" style="width: 620px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/updatelistitems-envelope1.png"><img class="size-full wp-image-1162" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/updatelistitems-envelope1.png" alt="UpdateListItems envelope" width="610" height="168" /></a><p class="wp-caption-text">UpdateListItems envelope</p></div>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> UpdateListPrice<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>
       <span style="color: #008000;">new</span> Uri<span style="color: #000000;">&#40;</span>siteUrl <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/_vti_bin/Lists.asmx&quot;</span>, UriKind.<span style="color: #0000FF;">Absolute</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    request.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span>
    request.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/soap+xml; charset=utf-8&quot;</span><span style="color: #008000;">;</span>
    request.<span style="color: #0000FF;">BeginGetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>RequestCallback<span style="color: #000000;">&#41;</span>,
       request<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> RequestCallback<span style="color: #000000;">&#40;</span>IAsyncResult asyncResult<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">string</span> envelope <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;see image&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
    query <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;{0}{1}&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    query <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>query, selectedProduct.<span style="color: #0000FF;">Id</span>, newListPriceString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    envelope <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>envelope, listName, query<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>asyncResult.<span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
    body <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">EndGetRequestStream</span><span style="color: #000000;">&#40;</span>asyncResult<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span> encoding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> formBytes <span style="color: #008000;">=</span> encoding.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>envelope<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    body.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>formBytes, <span style="color: #FF0000;">0</span>, formBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    body.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    request.<span style="color: #0000FF;">BeginGetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>ResponseCallback<span style="color: #000000;">&#41;</span>, request<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ResponseCallback<span style="color: #000000;">&#40;</span>IAsyncResult asyncResult<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>asyncResult.<span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
    HttpWebResponse response <span style="color: #008000;">=</span>
        <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">EndGetResponse</span><span style="color: #000000;">&#40;</span>asyncResult<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Stream content <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">==</span> HttpStatusCode.<span style="color: #0000FF;">OK</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
       <span style="color: #0600FF;">try</span>
       <span style="color: #000000;">&#123;</span>
           <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Dispatcher</span>.<span style="color: #0000FF;">BeginInvoke</span><span style="color: #000000;">&#40;</span>ProcessResponse<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #000000;">&#125;</span>
       <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ProcessResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    selectedProduct.<span style="color: #0000FF;">ListPrice</span> <span style="color: #008000;">=</span> newListPrice<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Because of the <strong>TwoWay</strong> binding mode set on the <strong>ListPriceTextBlock</strong> on the list box, the price change is propagated to the item in the list box during the execution of the <strong>ProcessResponse</strong> method.</p>
<p>Build the Silverlight application and copy the resulting .xap file to your SharePoint project. I copied it to a Resources sub directory.</p>
<h2>Deploying the solution</h2>
<p>Now that it comes to the deployment of the SharePoint web part and its Silverlight application, you have to make some modifications to the Web Part files that contain the metadata for the Web Part.</p>
<p>Set the properties of the embedded silverlight resource. Include the Resources folder and the .xap file to the Web Part project. In the file properties set the Build action to <strong>Embedded Resource</strong>.</p>
<div id="attachment_1131" class="wp-caption aligncenter" style="width: 366px"><a href="http://sharepointmagazine.net/wp-content/uploads/2008/09/compile-as-embedded-resource1.png"></a><img class="size-full wp-image-1131" src="http://sharepointmagazine.net/wp-content/uploads/2008/09/compile-as-embedded-resource1.png" alt="Compile as an embedded resource" width="356" height="154" /><p class="wp-caption-text">Compile as an embedded resource</p></div>
<p>Build the SharePoint project and open the DLL with a tool like Reflector in order to copy the right namespace of the embedded resource which is the silverlight application. Return to the Web Part project and open the Web Part class. Before the namespace declaration you have to add the metadata attribute that enables an embedded resource in an assembly:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>assembly<span style="color: #008000;">:</span> WebResource<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;SL.AdventureWorksProducts.Resources.SL.XAML.AdventureWorksProducts.xap&quot;</span>, <span style="color: #666666;">&quot;application/x-silverlight-app&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">namespace</span> SL.<span style="color: #0000FF;">AdventureWorksProducts</span>
<span style="color: #000000;">&#123;</span>
   … Web Part code …
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In the <strong>CreateChildControls</strong> method set the source property of the silverlight control to the location of the silverlight application. You can retrieve this location by using the <strong>GetWebResourceUrl</strong> method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">silverlightControl.<span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Page</span>.<span style="color: #0000FF;">ClientScript</span>.<span style="color: #0000FF;">GetWebResourceUrl</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;SL.AdventureWorksProducts.Resources.SL.XAML.AdventureWorksProducts.xap&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If needed you can modify the .webpart file and the .xml file to set certain Web Part properties.</p>
<p>When working with the Visual Studio 2008 extensions for WSS you can set the <strong>Start browser with URL</strong> property of the web part project to the URL of your SharePoint site. This will deploy the Web Part to this URL.</p>
<p>When deployed you can add the Web Part to a web part page. Don&#8217;t forget to configure your SharePoint server for enabling Silverlight! If you don&#8217;t know how to do that, you can always check the document &#8211; which is part of the Silverlight BluePrint for SharePoint &#8211;  included with this article.</p>
<h2>Conclusion</h2>
<p>If you want to learn more about Silverlight integration in SharePoint you can download the Silverlight BluePrint for Sharepoint at <a href="http://codeplex.com/SL4SP"></a>http://codeplex.com/SL4SP.</p>
]]></content:encoded>
			<wfw:commentRss>http://sharepointmagazine.net/technical/development/creating-a-sharepoint-web-part-that-hosts-a-data-binding-silverlight-application/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
