The last blog post I wrote was on interacting with SharePoint using services. One of the things that I’ve been quite involved in over the past year or so has been Silverlight and SharePoint integration. You will definitely see more of this type of development, so building off of my last post I decided to write a blog on consuming a web service with SharePoint.

That said, this blog post will walk you through how to create a Web service that integrates with the SharePoint object model and then uses that service in a Silverlight application (which we’ll embed within SharePoint). It is an excerpt from a wider Silverlight and SharePoint 1-day training that I’ve been building at work (with the help of Advaiya). It also complements a book Paul Stubbs and I just wrote called “Professional SharePoint 2007 Development using Silverlight 2.” You can check this book out here: http://www.amazon.com/Professional-Microsoft-SharePoint-Development-Silverlight/dp/0470434007/ref=sr_1_1?ie=UTF8&s=books&qid=1234663767&sr=8-1.

Note: I will cover WCF, Siverlight and SharePoint in a future blog post.

Your Environment

Before you begin, make sure you’ve got the following environment set up:

1.  Your web.config supports Silverlight. Check out the SIlverlight and SharePoint Blueprint (http://www.codeplex.com/SL4SP) for more information as well as the recent MSDN article on this subject (http://msdn.microsoft.com/en-us/magazine/dd148643.aspx).

2. Windows Server 2003, 2008, Silverlight runtime, Silverlight Tools for VS 2008 SP 1, VS 2008 SP1, and VSeWSS 1.2.

Building the Service

The first thing you need to do is build the Web service. The Web service will be an ASMX-based web service (i.e. ASP.NET). (We will cover WCF-based services and Silverlight later on in the workshop.) To create the service, open Visual Studio 2008. Select File, New Web Site and then select ASP.NET Web Service. Choose the File System as the location and then select the language (e.g. Visual C#) and provide a path for the project. Click OK. Figure 1 provides a screenshot of the New Web Site dialog.

clip_image002

Figure 1: New Web Site Dialog

In Solution Explorer, change the names of the .asmx and .cs service files from the default Service.asmx and Service.cs (in the App_Code folder) to MyFirstSPService.asmx and MyFirstSPService.cs.

clip_image004

Figure 2: Renaming the Service Files

You’ll also need to update the class references and statements in the newly named files. For example, in MyFirstSPService.asmx ensure you have the following code:

<%@ WebService Language="C#" CodeBehind="~/App_Code/MyFirstSPService.cs" Class="MyFirstSPService" %>

And in the MyFirstSPService.cs file, ensure you have the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class MyFirstSPService : System.Web.Services.WebService

{

public MyFirstSPService () {

//Uncomment the following line if using designed components

//InitializeComponent();

}

[WebMethod]

public string HelloWorld() {

return "Hello World";

}

}

To make sure that your solution compiles, hit F5 or select Debug, Start Debugging on the Visual Studio 2008 menu. You may be prompted with the dialog in Figure 3. If you are, click OK.

clip_image006

Figure 3: Debugging Not Enabled

If your solution successfully compiles and debugs, you will be presented with the following debug screen.

clip_image008

Figure 4: Debug Screen

In the debug screen, you will see a link at the top representing the one web method (HelloWorld) that is available within the service. Click the HelloWorld link and then to test the service click the Invoke button—which returns a string with the value: “Hello World.” See Figure 5.

clip_image010

Figure 5: Invoking the Hello World Web Method

Now that you’ve tested the default service that is created for you when you create a Web service, go back to the Visual Studio project and replace the HelloWorld web method code with the following web method:

[WebMethod]

public void useNormalMOSSApi(string SalesSPSite, string productName, string productNumber, string FY08Sales)

{

string strDashListRoot = SalesSPSite;

using (SPSite site = new SPSite(strDashListRoot))

{

using (SPWeb web = site.OpenWeb())

{

web.AllowUnsafeUpdates = true;

SPList list = web.Lists["FY 09 Sales"];

SPListItem Item = list.Items.Add();

Item["Title"] = productName;

Item["ProductNum"] = productNumber;

Item["Sales"] = FY08Sales;

Item.Update();

}

}

}

This code essentially enables you to set the context for a SharePoint web site and then add data to three different columns (Title, ProductNum and Sales) in a list called FY 09 Sales.

When you add this code, you’ll get errors. This is because you need to add a reference to the Microsoft.SharePoint.dll. To do this, right-click the project name, select Add Reference and then select Windows SharePoint Services (as is shown in Figure 6) and click OK.

clip_image012

Figure 6: Add Reference Dialog

Note: In order for this service to work, you’d need to have created a custom list in SharePoint called “FY 09 Sales” with three columns of type Text called “Title,” “ProductNum,” and “Sales.” To do this, go to your SharePoint site and click View All Site Content at the root level site and then click Create. In the Custom Lists category, click Custom List. Make the Name “FY 09 Sales” and add a description to the Description field. When the list is created it will open by default. Click Settings and then select Create Column. Add each of the three aforementioned columns setting the type of information in the column to Single lines of text—see Figure 7.

clip_image014

Figure 7: Setting the Column Type

Getting back to the service, now that you’ve added the reference to the SharePoint DLL, add a using statement at the top of the class as follows:

using Microsoft.SharePoint;

The code errors should go away. The full MyFirstSPService.cs file should look like the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using Microsoft.SharePoint;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class MyFirstSPService : System.Web.Services.WebService

{

public MyFirstSPService () {

//Uncomment the following line if using designed components

//InitializeComponent();

}

[WebMethod]

public void useNormalMOSSApi(string SalesSPSite, string productName, string productNumber, string FY08Sales)

{

string strDashListRoot = SalesSPSite;

using (SPSite site = new SPSite(strDashListRoot))

{

using (SPWeb web = site.OpenWeb())

{

web.AllowUnsafeUpdates = true;

SPList list = web.Lists["FY 09 Sales"];

SPListItem Item = list.Items.Add();

Item["Title"] = productName;

Item["ProductNum"] = productNumber;

Item["Sales"] = FY08Sales;

Item.Update();

}

}

}

}

To test this, build the web site solution. Hit F5 and then test out the web method.

Note: You must create the SharePoint list before you can successfully test out the service.

clip_image016

Figure 8: Testing the Service Call

If your service is successful, you should have a result similar to the following in your SharePoint list:

clip_image018

Figure 9: Service Result in SharePoint

Deploying the Service

Now that you’ve built the service, you need to deploy it. We’ll deploy it to IIS 7.0. If you’re using another version of IIS, the general process you’ll need to follow is to 1) Create a web site, 2) map the virtual path to the root web site path of the service, and 3) set permissions for the service.

Open IIS and then expand the Connections until you see the web sites. Right-click Web Sites and select Add Web Site. Give the site a name, for example MyFirstSPServiceIISSite and then map the physical path of the service you just created to the IIS web site. See Figure 10. Assign an unused port.

Note: To test the service out on the local environment, click Test Settings. This will tell you if the service is reachable. If you do not get to successful tests, you’ll need to adjust the Connect as… settings. One way is to explicitly set the calling service with your (that is the local Windows credentials) to make sure the call will succeed. You then also need to go to the Authentication option in the Features view and set Windows Authentication to Enabled. In a production environment, you’ll need to use different settings—potentially having a user (e.g. IIS_User) for your service and enabling ASP.NET Authentication.

clip_image020

Figure 10: Creating the IIS Site

When you‘ve done this, click View, Refresh and then select MyFirstSPServiceIISSite in the Web Sites list and select Content View. You should see something similar to Figure 11. To make sure you’ll be able to

clip_image022

Figure 11: Content View of Web Site in IIS

At this point, you’ll be able to right-click the MyFirstSPService.asmx file and select Browse. Test out the web method again to make sure you have another successful call in the service to the SharePoint site—see Figure 12.

clip_image024

Figure 12: Second Successful Call

While all the service testing at each step in the development process may seem monotonous, it is recommended as a good defensive programming practice. This way, if the service fails you have a better idea of where in the process it failed.

Building the Silverlight and SharePoint Application

Now that you’ve created the service, you can now create the client side application, which is the Silverlight application that will eventually be deployed to SharePoint. Let’s first create the Silverlight application.

Open Visual Studio 2008 and create a new solution, using the Blank Solution template. To do this, click File, New Project, and then select the Visual Studio Solutions and Blank Solution template. Provide a name for your solution and then click OK.

clip_image026

Figure 13: Creating a Blank Solution

Right-click the solution and then select Add ,New Project, and then in the Project Types category select Silverlight. In the Templates category select Silverlight Application and provide a name for your application, for example SPSilverlightClient.

clip_image028

Figure 14: New Silverlight Application

When prompted, just select Automatically generate a test page… as the test environment for the Silverlight application. You do not need the full web site for this solution. Navigate to the Page.xaml file and add the following XAML code to create your Silverlight UI.

<UserControl x:Class="SPSilverlightClient.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="750" Height="380">

<Canvas x:Name="LayoutRoot">

<Canvas Width="550" Height="300">

<Path Width="550" Height="300" Canvas.Left="38" Canvas.Top="9" Stretch="Fill"

Data="F1 M 42.336,8.86813L 257.336,8.86813C 259.545,8.86813 261.336,10.659 261.336,12.8681L 261.336,77.8681C 261.336,80.0773 259.545,81.8681 257.336,81.8681L 42.336,81.8681C 40.1269,81.8681 38.336,80.0773 38.336,77.8681L 38.336,12.8681C 38.336,10.659 40.1269,8.86813 42.336,8.86813 Z ">

<Path.Fill>

<LinearGradientBrush StartPoint="0.75,0.7" EndPoint="0.75,-0.01">

<LinearGradientBrush.GradientStops>

<GradientStop Color="Azure" Offset="0"/>

<GradientStop Color="#FFDCDCDC" Offset="0.8"/>

<GradientStop Color="#FFC3C3C3" Offset="1"/>

</LinearGradientBrush.GradientStops>

</LinearGradientBrush>

</Path.Fill>

</Path>

</Canvas>

<TextBlock

Canvas.Left="50"

Canvas.Top="15"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="18"

Text="Sales Information for FY 09"

Foreground="#FFF3F3F3" />

<TextBlock

Canvas.Left="51"

Canvas.Top="16"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="18"

Text="Sales Information for FY 09"

Foreground="#FF666666" />

<!-- User entry form text and controls.-->

<TextBlock x:Name="txtBlckSites"

Canvas.Left="50"

Canvas.Top="50"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="Available SharePoint Sites: "

Foreground="#FF666666"/>

<ComboBox x:Name="cmboBoxOfferings"

Canvas.Left="210"

Canvas.Top="50"

Height="20"

Width="290"

DropDownClosed="cmboBoxOfferings_DropDownClosed">

<ComboBoxItem x:Name="optionOne">

<TextBlock x:Name="txtSPDev">

http://stefoxdemo

</TextBlock>

</ComboBoxItem>

<ComboBoxItem x:Name="optionTwo">

<TextBlock x:Name="txtVSDev">

http://sharepoint/sites/spotw

</TextBlock>

</ComboBoxItem>

<ComboBoxItem x:Name="OptionThree">

<TextBlock x:Name="txtSQLSRVDev">

http://sharepoint/sites/sp14

</TextBlock>

</ComboBoxItem>

<ComboBoxItem x:Name="OptionFour">

<TextBlock x:Name="txtAdvSPDev">

http://my/sites/stefox

</TextBlock>

</ComboBoxItem>

<ComboBoxItem x:Name="OptionFive">

<TextBlock x:Name="txtClientDev">

http://sharepoint/sites/signup

</TextBlock>

</ComboBoxItem>

</ComboBox>

<TextBlock

Canvas.Left="50"

Canvas.Top="90"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="Site Description: "

Foreground="#FF666666" />

<TextBlock x:Name="txtSiteDescr"

Canvas.Left="210"

Canvas.Top="90"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="Description Here."

Foreground="Black"

Width="290"

TextWrapping="Wrap"/>

<TextBlock

Canvas.Left="50"

Canvas.Top="130"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="Product Name: "

Foreground="#FF666666"/>

<TextBox x:Name="txtProductName"

Height="25"

Width="200"

Canvas.Left="210"

Canvas.Top="125"/>

<TextBlock

Canvas.Left="50"

Canvas.Top="170"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="Product Number: "

Foreground="#FF666666"/>

<TextBox x:Name="txtProductNum"

Height="25"

Width="200"

Canvas.Left="210"

Canvas.Top="165"/>

<TextBlock

Canvas.Left="50"

Canvas.Top="215"

FontFamily="Trebuchet MS"

FontWeight="Bold" FontSize="12"

Text="FY 09 Sales: "

Foreground="#FF666666"/>

<TextBox x:Name="txtFY08Sales"

Height="25"

Width="200"

Canvas.Left="210"

Canvas.Top="210"/>

<Button x:Name="btnWebServiceCall"

Height="25"

Width="60"

Content="Insert"

Canvas.Left="125"

Canvas.Top="260"

Click="btnWebServiceCall_Click"/>

<Button x:Name="btnFieldsClear"

Height="25"

Width="60"

Content="Clear"

Canvas.Left="215"

Canvas.Top="260"

Click="btnFieldsClear_Click"/>

</Canvas>

</UserControl>

When complete, your Silverlight client UI should similar to Figure 15.

clip_image030

Figure 15: Silverlight Client

Notice that there are a number of key events that are associated with some of the controls in the XAML code. These correspond to code-behind events in the Page.xaml.cs file. Before we jump to the code-behind, add a reference to the service you created earlier. To do this, right click References and select Add Service Reference. To get the service URL, you can go into your IIS view, right-click the ASMX file and select Browse. Copy and paste the URL from the browser to the Address field in the Add Service Reference dialog—see Figure 16.

clip_image032

Figure 16: Adding a Service Reference

With the service added to the Silverlight client application, you can now add the code-behind that maps to the Silverlight UI.

The first thing you’ll need is four class-level variables, which represent the data that we’re going to pass to the web method in our service.

string productName = "";

string productNumber = "";

string FY08Sales = "";

string SalesSPSite = "";

Then, you’ll need to add the other logic, which in the code to follow includes an event from the Insert button in the Silverlight UI and some logic that maps to the Clear button.

The web service call, which is triggered from the button, looks like the following:

private void btnWebServiceCall_Click(object sender, RoutedEventArgs e)

{

productName = txtProductName.Text;

productNumber = txtProductNum.Text;

FY08Sales = txtFY08Sales.Text;

SPSilverlightClient.ASMXUpdateSPList.MyFirstSPServiceSoapClient proxy = new SPSilverlightClient.ASMXUpdateSPList.MyFirstSPServiceSoapClient();

proxy.useNormalMOSSApiCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_useNormalMOSSApiCompleted);

proxy.useNormalMOSSApiAsync(SalesSPSite, productName, productNumber, FY08Sales);

proxy.CloseAsync();

}

Note that Silverlight service calls are asynchronous, so you need to have a completed event, which in this application looks like the following:

void proxy_useNormalMOSSApiCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

MessageBox.Show("Can you believe it? This stuff actually works!");

}

The code here obviously doesn’t do much other than issue a message to the user when the service call is complete. In production code, you would probably want to verify that there had been an update to the SharePoint list before issuing this type of message.

The btnFieldsClear_Click event, shown below, simply clears the user entries in the Silverlight UI.

private void btnFieldsClear_Click(object sender, RoutedEventArgs e)

{

txtFY08Sales.Text = "";

txtProductName.Text = "";

txtProductNum.Text = "";

}

You may also notice that there is an event that corresponds to the closing of the drop-down box. This is essentially place-holder code, but in reality should grab the text property of the selected item within the combo box and assign that value to SalesSPSite. However, since I only have one site to demo, I hard-coded the variable to always be that site.

private void cmboBoxOfferings_DropDownClosed(object sender, EventArgs e)

{

//Be warned...lazy hard-coded variable data to follow.

if (optionOne.IsSelected == true)

{

txtSiteDescr.Text = "FY 09 Sales Tracking Site";

SalesSPSite = "http://stefoxdemo";

}

else if (optionTwo.IsSelected == true)

{

txtSiteDescr.Text = "SharePoint on the Web Metrics";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionThree.IsEnabled == true)

{

txtSiteDescr.Text = "Wave 14 Reports";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionFour.IsEnabled == true)

{

txtSiteDescr.Text = "Personal Metrics";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionFive.IsEnabled == true)

{

txtSiteDescr.Text = "Signage Reports";

SalesSPSite = "http://stefoxdemo";

};

}

If you were to look at the code-behind in its entirety, this is what it would look like:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SPSilverlightClient

{

public partial class Page : UserControl

{

string productName = "";

string productNumber = "";

string FY08Sales = "";

string SalesSPSite = "";

public Page()

{

InitializeComponent();

}

private void btnWebServiceCall_Click(object sender, RoutedEventArgs e)

{

productName = txtProductName.Text;

productNumber = txtProductNum.Text;

FY08Sales = txtFY08Sales.Text;

SPSilverlightClient.ASMXUpdateSPList.MyFirstSPServiceSoapClient proxy = new SPSilverlightClient.ASMXUpdateSPList.MyFirstSPServiceSoapClient();

proxy.useNormalMOSSApiCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_useNormalMOSSApiCompleted);

proxy.useNormalMOSSApiAsync(SalesSPSite, productName, productNumber, FY08Sales);

proxy.CloseAsync();

}

void proxy_useNormalMOSSApiCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

MessageBox.Show("Can you believe it? This stuff actually works!");

}

private void btnFieldsClear_Click(object sender, RoutedEventArgs e)

{

txtFY08Sales.Text = "";

txtProductName.Text = "";

txtProductNum.Text = "";

}

private void cmboBoxOfferings_DropDownClosed(object sender, EventArgs e)

{

//Be warned...lazy hard-coded variable data to follow.

if (optionOne.IsSelected == true)

{

txtSiteDescr.Text = "FY 09 Sales Tracking Site";

SalesSPSite = "http://stefoxdemo";

}

else if (optionTwo.IsSelected == true)

{

txtSiteDescr.Text = "SharePoint on the Web Metrics";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionThree.IsEnabled == true)

{

txtSiteDescr.Text = "Wave 14 Reports";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionFour.IsEnabled == true)

{

txtSiteDescr.Text = "Personal Metrics";

SalesSPSite = "http://stefoxdemo";

}

else if (OptionFive.IsEnabled == true)

{

txtSiteDescr.Text = "Signage Reports";

SalesSPSite = "http://stefoxdemo";

};

}

}

}

At this point, you are now done building the Silverlight UI. Let’s move on to the final part: building the SharePoint web part.

Building the SharePoint Web Part Host Container

The SharePoint web part will serve as the object within SharePoint that will simply put host your Silverlight application. There are a number of ways to do this, of which this is one.

Open the solution file (where you created your Silverlight application) and then right-click the solution and select Add, and New Project. Select the SharePoint project type and then select Web Part. Provide a name for your Web Part project.

clip_image034

Figure 17: Adding SharePoint Web Part

When your project is created, right click References and select Add Reference. Add the System.Web.Extensions DLL and System.Web.Silverlight DLL and click OK. See Figure 18

clip_image036

Figure 18: Adding Silverlight References

In the WebPart1.cs file (I removed and re-added a new Web Part item and called mine MyFirstSPServiceCall), you’ll need to have the following code. This checks for a ScriptManager object and also sets some properties for the Silverlight control we’ll add to the Controls collection.

protected override void OnLoad(EventArgs e)

{

base.OnLoad(e);

ScriptManager sm = ScriptManager.GetCurrent(this.Page);

if (sm == null)

{

sm = new ScriptManager();

Controls.AddAt(0, sm);

}

}

protected override void CreateChildControls()

{

base.CreateChildControls();

System.Web.UI.SilverlightControls.Silverlight ctrl = new System.Web.UI.SilverlightControls.Silverlight();

ctrl.ID = "InsertSPListItem";

ctrl.Source = "http://stefoxdemo/XAPS1/SPListClient.xap";

ctrl.Width = new Unit(650);

ctrl.Height = new Unit(400);

Controls.Add(ctrl);

}

You can edit the XML properties of your web part through the WSP View. For example, the following XML is from the .webpart XML file. You can see I’ve updated the name and description of the web part with something a little more intuitive.

<?xml version="1.0" encoding="utf-8"?>

<webParts>

<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

<metaData>

<!--

The following Guid is used as a reference to the web part class,

and it will be automatically replaced with actual type name at deployment time.

-->

<type name="967945ef-8baa-406e-bda3-fe4b836b2f66" />

<importErrorMessage>Cannot import MyFirstSPServiceCall Web Part.</importErrorMessage>

</metaData>

<data>

<properties>

<property name="Title" type="string">My First SharePoint Service Call Web Part</property>

<property name="Description" type="string">A web part that hosts a call into SharePoint to update a list.</property>

</properties>

</data>

</webPart>

</webParts>

Jumping back to the code for the actual web part, the complete code that you’ll need to have in the webpart.cs file (in my case MyFirstSPServiceCall.cs) is as follows:

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace SPSilverlightSPWP

{

[Guid("967945ef-8baa-406e-bda3-fe4b836b2f66")]

public class MyFirstSPServiceCall : System.Web.UI.WebControls.WebParts.WebPart

{

public MyFirstSPServiceCall()

{

}

protected override void OnLoad(EventArgs e)

{

base.OnLoad(e);

ScriptManager sm = ScriptManager.GetCurrent(this.Page);

if (sm == null)

{

sm = new ScriptManager();

Controls.AddAt(0, sm);

}

}

protected override void CreateChildControls()

{

base.CreateChildControls();

System.Web.UI.SilverlightControls.Silverlight ctrl = new System.Web.UI.SilverlightControls.Silverlight();

ctrl.ID = "InsertSPListItem";

ctrl.Source = "http://stefoxdemo/XAPS1/SPListClient.xap";

ctrl.Width = new Unit(650);

ctrl.Height = new Unit(400);

Controls.Add(ctrl);

}

}

}

At this point, you need to ensure the Debug property in your SharePoint project properties is set to your http://localhost (or the correct server to which you’re deploying the web part), and you can deploy the web part.

If you notice in the source property code of the Silverlight application, it references an absolute URL on a test server. This presupposes two things: 1) you’ve created a document library called XAPS1 and 2) you’ve added the XAP file (the compiled Silverlight application) into that document library. If you have not, then you will simply get a blank, white web part. Also, note that this is not the only way to do this. In Professional SharePoint 2007 Development using Silverlight 2, there are a number of ways to deploy the web part that are discussed.

To deploy the web part, right-click the SharePoint project and select Deploy.

Testing the Web Part

Now that you’ve deployed the web part, you can now open your SharePoint server and add the new Silverlight web part to your SharePoint site. To do this, click Site Actions and Edit Page. Click Add a Web and then select your newly deployed web part from the Web gallery. Once you’ve added the web part, you can now interact with the Silverlight UI within SharePoint that further interacts with your SharePoint list (i.e. that SharePoint object model).

clip_image038

Figure 19: Silverlight Web Part in SharePoint

When you click Insert, a new record will be added to the SharePoint list and a message will be issued, as coded in our Silverlight UI, to the user.

clip_image040

Figure 20: Message Indicating Success

Of course the success message is only one indication. To verify that a new record was indeed added to the FY 09 Sales , navigate to the list and verify the new information on the list.

clip_image042

Figure 21: Successful Addition of Record

For more information on how to integrate Silverlight and SharePoint, check out Professional SharePoint 2007 Development using Silverlight 2 and http://mssharepointdeveloper.com.