Blog

Integration of Silverlight and Sharepoint 2007

By David Espino

What is Silverlight?

 

Silverlight is a cross-browser, cross-platform and cross-device browser plug-in that helps companies design, develop and deliver applications and experiences on the Web.

For further information visit:

http://en.wikipedia.org/wiki/Silverlight

What is Sharepoint 2007?

 

Microsoft Office SharePoint Server 2007 is an integrated suite of server capabilities that can help improve organizational effectiveness by providing comprehensive content management and enterprise search, accelerating shared business processes, and facilitating information-sharing across boundaries for better business insight.

Strong and WeakPoints

 

Silverlight Strengths are:

·         Handling Video and Audio.

·         Design Tools

·         No license required

·         Rich Interface Controls

·         Deep Zoom

 

Sharepoint Strengths are:

·         Search Features

·         Documentation and Versioning

·         Complex workflow business process

·         Integration with Infopath Forms

·         Focus on Collaboration

 

Silverlight Weaknesses are:

·         Limited  and small CLR

·         Limited Communication between XAP’s

·         Design and UI oriented

 

Silverlight Weaknesses are:

·         Business oriented only

·         No fancy controls (limited to CSS)

·         Lists, Lists, Lists

What if we combine?

 

What if we combine the features of a Rich interface with animations which allows a user to interact with the system in a more friendly way, using business power and collaboration from Sharepoint and also using the security provided by Sharepoint.

Results in:

·         Rich Interface Application

·         Business Application  with great UI

·         Security managed by sharepoint

·         No need for abc screens

·         Easy to upgrade and rehuse components

·         Controls Available for all sites of server

·         And So On..

... So what do we need?

 

A .xap file

A .xap file is basically a compiled Silverlight application, it contains the Control to be displayed in Sharepoint Pages. The code for this example is going to extract information from a Sharepoint List. To achieve this, the code in xap file will use a WebClient object and its method DowloadString. This method Downloads the specified resource as a String or a Uri.

 The WebClient will invoke the HttpHandler to retrieve the information from the Sharepoint site published as raw xml.

public void Page_Loaded(object o, EventArgs e)

        {

            // Required to initialize variables

            InitializeComponent();

 

            try

            {

               

 

                WebClient client = new WebClient();

                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

                client.DownloadStringAsync(new Uri(UrlSitio, UriKind.Absolute));

 

              

 

            }

            catch (Exception ex)

            {

                this.Details.Text += ex.ToString();

            }

        }

 

   void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

            if (e.Error == null)

            {

                //Process your data

  }

 

 

        }

The .xap file is actually a .zip file that contains all the files necessary for the application. Just rename the .xap file to have a .zip extension and then you can open it up to see its contents. Just try it yourself.

1

The .xap file contains an application manifest (AppManifest.xaml) file and all the necessary DLL's that are required by the application. The first DLL contained is the compiled version of you application and has the same name of your application. In my test I created an application named "SilverlightApplication1", so the DLL is named "SilverlightApplication1.dll". The rest of the DLL's are the dependencies the application requires.

The first element in the AppManifest.xaml is a Deployment node. This node defines the application and contains child AssemblyPart nodes. There are other AssemblyPart nodes, each one define what assemblies (DLLs) are contained within the .xap file, and give each of them a name.

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="MapaSitio" EntryPointType="MapaSitio.App" RuntimeVersion="3.0.40624.0">

  <Deployment.Parts>

    <AssemblyPart x:Name="MapaSitio" Source="MapaSitio.dll" />

    <AssemblyPart x:Name="Microsoft.Expression.Interactions" Source="Microsoft.Expression.Interactions.dll" />

    <AssemblyPart x:Name="System.Windows.Interactivity" Source="System.Windows.Interactivity.dll" />

    <AssemblyPart x:Name="System.Xml.Linq" Source="System.Xml.Linq.dll" />

  </Deployment.Parts>

</Deployment>

Now, if you look back up to the top, you'll see the Deployment node has EntryPointAssembly and EntryPointType attributes. The EntryPointAssembly attribute defines which assembly defined below (as a child AssemblyPart node) is the main assembly (DLL) for the application. And, the EntryPointType attribute specifies the Class contained within the assembly (DLL), defined in the EntryPointAssembly attribute, is the main class that will be instantiated to start the application.

The Deployment node also has a RuntimeVersion attribute that defines the version of Silverlight the application is built for.

 

A sharepoint site with an HttpHandler

An HTTPHandler does really is take an HTTPContext object and work out what to do with it. Although that is a very simple analogy of them, the best way to describe it would be with the following example:

 

<%@ WebHandler Language="C#"  Class="GetXMLHandler" %>

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>

 

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.Services;

using Microsoft.SharePoint;

 

public class GetXMLHandler : IHttpHandler

    {

 

        public void ProcessRequest(HttpContext context)

        {

            string SiteUrl = context.Request.QueryString["SiteUrl"];

            string strList = context.Request.QueryString["List"];

            string strView = context.Request.QueryString["View"];

 

            Guid ListID = new Guid(strList);

            Guid ViewID = new Guid(strView);

 

            using (SPSite mySite = new SPSite(SiteUrl))

            {

                using (SPWeb myWeb = mySite.OpenWeb())

                {

                    SPList ListObject = myWeb.Lists[ListID];

                    SPView ViewObject = Lista.Views[ViewID];

                    SPListItemCollection Items = ListObject.GetItems(ViewObject);

 

                    context.Response.Clear();

                    context.Response.ContentType = "text/xml";

                    context.Response.Write(Items.Xml);

                }

            }

        }

       

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

     }

Usually you associate an HTTPHandler with either a new file type (eg. ".AGASP") or with a .ASHX file. In the pipeline, the Handler is called last and when it is called, the appropriate HTTPHandler is executed. The handler can print information out or analyze information. The HTTPApplication object will call the HTTPHandler to process the request and generate a response.

Two Security Files

Silverlight 2 and later includes support for cross-domain connectivity which allows an application to access resources from locations other than the site of origin. This is an important feature for enabling Silverlight applications to consume existing services on the web. The security policy system in the Silverlight runtime requires that a policy file be downloaded from a target domain before a network connection is allowed access to a network resource under that target domain. This security policy system affects cross-domain network access for the WebClient and HTTP classes in the System.Net namespace. Network connections for the WebClient and HTTP classes to the site or host of origin do not require a security policy.

 

 

A Content Webpart

Nothing more than html generated indicating the control to load and the input parameters. The content can be the same as the html for test page generated in website consuming our Silverlight control.

 

 

    <div id="silverlightControlHost">

            <object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">

                  <param name="source" value="ClientBin/MapaSitio.xap"/>

                  <param name="onerror" value="onSilverlightError" />

                  <param name="background" value="white" />

                  <param name="minRuntimeVersion" value="3.0.40624.0" />

                  <param name="autoUpgrade" value="true" />

                  <param name="initParams" value="SiteUrl=http://mysite.com?SiteSubDir=http://mysite.com/subdir/" />

                  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none;">

                  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

                  </a>

            </object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>

 

Now we have to follow the next steps to achieve the integration between a Silverlight xap and a Sharepoint 2007 site.

1.       Save the .xap file. The destination folder should be  C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\ or a subfolder inside that folder.

2.       Copy the security files. The security files should be located on the Root folder of Sharepoint Site. By default should be C:\inetpub\wwwroot.

3.       Save the HttpHandler file. Save the handler in the folder _app_bin located inside the virtual directory folder of your Sharepoint Site.

4.       Create the content webpart. Create a content webpart within a Sharepoint page, call the control you just added. Be careful with the <param name="source" , check the path corresponding to the .xap file you just copied.

This will allow you to see your Silverlight control embedded within the Sharepoint’s page.

 

 

Comments

Leave a comment

 
 
 
 
CAPTCHA Image Validation