Exercise 1: Loading the Assemblies Dynamically

This exercise shows how convert an existing project from one that includes the client object model assemblies within its XAP to one that loads them dynamically.

Task 1 – Modifying The Assembly Properties

In this task, you will change the properties of the two SL client object model assemblies to prevent them from being added to the XAP file.

  1. Using Visual Studio 2010, open the starter solution named DynamicXAP from the <Install>\Labs\DynamicXAPLoading\source\begin\ folder. Solution Explorer should look like this:

    Figure 1

    DynamicXAP solution

  2. Notice that there are two projects in this solution:
    1. DynamicXAP – this is the actual project that will be deployed to SharePoint. It is an Empty SharePoint Project with a new project item added to it - the WebPart1 item just above key.snk in the picture.
    2. SilverlightApplication1 – this is the Silverlight project in which we will build our Silverlight application. The output of this project is packaged with the output of the DynamicXAP project and deployed to SharePoint.
  3. [Optional] If desired, explore the different files in both projects. This lab doesn’t spend any time on the actual Silverlight application that is included in the starter project, so if you’d like to see what the project contains, feel free to take a look.
  4. Open the References node in Solution Explorer
  5. Right-click on the reference for the assembly Microsoft.SharePoint.Client.Silverlight and select Properties from the context menu.
  6. From the Properties window, change the CopyLocal value to False:

    Figure 2

    Properties window

  7. Repeat step 5 for the assembly reference to Microsoft.SharePoint.Client.Silverlight.Runtime.
  8. NOTE: We still need the references to the two assemblies in our project so it will compile and so we get Intellisense support.

Task 2 – Changing The Code

In this task you will update the starter code to programmatically download the client object model assemblies.

  1. Before beginning, Build the Solution and check the size of the XAP file by opening Windows Explorer and browsing to <Install Directory>\Labs\DynamicXAPLoading\ Source\Begin\SilverlightApplication1\Bin\Debug. The file SilverlightApplication1.xap should be 263 KB in size.
  2. From Solution Explorer, in the SilverlightApplication1 project, double-click the file MainPage.xaml.cs to open it and display the code behind for the Silverlight application.
  3. Locate the method MainPage_Loaded and delete the call to the LoadList method. The MainPage_Loaded method should now be empty.
  4. Add the following code to MainPage_Loaded:

    C#

    LoadClientOM loadClientOM = new LoadClientOM(delegate() { LoadList(); }); loadClientOM.Run();

  5. This code instantiates a new object and passes in a delegate to the LoadLists method. The new object (which we’ll add in Task 3) will handle the downloading of the client object model assemblies and will then call the LoadLists delegate.

Task 3 – Including The LoadClientOM Object

The object that will actually perform the work to load the client object model assemblies is in the folder with the starter project but is not included as part of the VS project. In this task you will add that file to the project and add code to the starter solution query SharePoint but control the size of the dataset returned.

  1. From Solution Explorer, click the button in the toolbar to show all files:

    Figure 3

    Showing all files

  2. Locate the file LoadClientOM.cs inside the SilverlightApplication1 project and right-click on it. Select Include In Project from the context menu:

    Figure 4

    Including LoadClientOM.cs

  3. Re-Build the Solution.
  4. Now re-check the size of the XAP file by opening Windows Explorer and browsing to <Install Directory>\Labs\DynamicXAPLoading\Source\Begin\SilverlightApplication1\Bin\Debug. The file SilverlightApplication1.xap should be 125 KB in size – 138 KB smaller than it was before.

Exercise 1 verification

In order to verify that you have correctly performed all steps of exercise 1, proceed as follows:

Verification 1

In this verification, you can compare the size of the XAP file built by Visual Studio If the XAP file is now 125 KB, you have completed the exercise successfully.