in

ASP.NET Weblogs

Tolga Koseoglu

Silverlight in MOSS 2007 (SP1)

First, I am trying to list, most likely not in logical order, all the steps necessary. After that I will support each point with either code or a screen shot.

  1. Develop a custom web part "wrapper". Configure so it can be deployed within MOSS.
  2. Create a simple Silverlight project. The two important files are Page.xaml and Page.cs (the code-behind file for the .xaml file)
  3. In your MOSS IIS directory create two custom folders. One for the Silverlight project .dll (ClientBin) and one for the .xaml (Xaml) files. Set permission level rather high and "Script only" on the execution properties for the ClientBin.
  4. Copy the infamous Silverlight.js in the yet another custom folder called Silverlight.

SilverlightMOSS_1

Code:

Web Part Wrapper:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Microsoft.SharePoint.WebPartPages;

using System.ComponentModel;

using System.Text;

 

namespace thelobbyWebParts

{

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

    {

        #region "Variable Declaration"

        private String sXaml = "Page.xaml";

        private const String sDefault = "";

        #endregion

 

        #region "Properties"

        [WebBrowsable(true), Personalizable(true)]

        public string Xaml

        {

            get

            {

                return sXaml;

            }

            set

            {

                sXaml = value;

            }

        }

        #endregion

     

        protected override void CreateChildControls()

        {

            try

            {

                this.Page.ClientScript.RegisterClientScriptInclude("Silverlight", "/CustomResources/Silverlight/Silverlight.js");

            }

            catch { }           

        }

  

        protected override void Render(System.Web.UI.HtmlTextWriter writer)

        {           

            try

            {

                //HTML DIV TO HOST THE SILVERLIGHT CONTENT                                                                          

                writer.Write("<div id='SilverlightControlHost' />");

 

                //SCRIPT START

                writer.Write("<script language='javascript' type='text/javascript'>");

              

                //CREATE SILVERLIGHT OBJECT

                writer.Write("Silverlight.createObject('/CustomResources/Xaml/" + this.Xaml.ToString() +"', document.getElementById('SilverlightControlHost'), 'AgControl1',");

                writer.Write("{width:'600px', height:'600px', inplaceInstallPrompt:false, background:'#FFFFFF', isWindowless:'false', framerate:'24', version:'1.1'},");

                writer.Write("{onError:null, onLoad:null},");

                writer.Write("null);");

 

                //ERROR HANDLING FUNTION

                writer.Write("function OnErrorEventHandler(sender, errorArgs)");

                writer.Write("{");

                writer.Write("    var errorMsg = 'Silverlight Error:';");

                writer.Write("    errorMsg += 'Error Type:    ' + errorArgs.errorType + '';");

                writer.Write("    errorMsg += 'Error Message: ' + errorArgs.errorMessage + '';");

                writer.Write("    errorMsg += 'Error Code:    ' + errorArgs.errorCode + '';");

                writer.Write("    switch(errorArgs.errorType)");

                writer.Write("    {");

                writer.Write("        case 'RuntimeError':");

                writer.Write("            if (errorArgs.lineNumber != 0)");

                writer.Write("            {");

                writer.Write("                errorMsg += 'Line: ' + errorArgs.lineNumber + '';");

                writer.Write("                errorMsg += 'Position: ' +  errorArgs.charPosition + '';");

                writer.Write("            }");

                writer.Write("            errorMsg += 'MethodName: ' + errorArgs.methodName + '';");

                writer.Write("            break;");

                writer.Write("        case 'ParserError':");

                writer.Write("            errorMsg += 'Xaml File:      ' + errorArgs.xamlFile      + '';");

                writer.Write("            errorMsg += 'Xml Element:    ' + errorArgs.xmlElement    + '';");

                writer.Write("            errorMsg += 'Xml Attribute:  ' + errorArgs.xmlAttribute  + '';");

                writer.Write("            errorMsg += 'Line:           ' + errorArgs.lineNumber    + '';");

                writer.Write("            errorMsg += 'Position:       ' + errorArgs.charPosition  + '';");

                writer.Write("break;");

                writer.Write("        default:");

                writer.Write("            break;");

                writer.Write("    }");

                writer.Write("    alert(errorMsg);");

                writer.Write("}");

 

                writer.Write("</script>");

 

 

               

                this.RenderChildren(writer);

            }

            catch

            {

            }

 

        }

    }

}

Page.xml

<Canvas xmlns="http://schemas.microsoft.com/client/2007"

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

            xmlns:app="clr-namespace:thelobbySilverlightProject;assembly=/CustomResources/ClientBin/thelobbySilverlightProject.dll"

        x:Name="rootCanvas"

        Loaded="Page_Loaded"

        x:Class="thelobbySilverlightProject.Page;assembly=/CustomResources/ClientBin/thelobbySilverlightProject.dll"

        Width="1200"

        Height="800"  

        >

 

      <TextBlock x:Name="txtTest" Canvas.Left="100" Text="Hello Everyone from Silverlight Project" FontSize="20"></TextBlock>

 

</Canvas>

Page.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Browser;

 

namespace thelobbySilverlightProject

{

    public partial class Page : Canvas

    {

        public void Page_Loaded(object o, EventArgs e)

        {

            // Required to initialize variables

            InitializeComponent();

 

            TextBlock txt = this.FindName("txtTest") as TextBlock;

            txt.Text = "I loaded in code-behind";

           

        }

 

    }

}

SilverlightMOSS_2 

If you have any question just let me know. I didn't explicitly go through the web part configuration, since that is a entire post in itself.

This post if for users who are already familiar with custom web part development in the SharePoint/MOSS environment.

I am going to post about custom web part development later.

As usual....Good Luck

--tolga--

Comments

 

Elliott said:

Wow. Pretty sweet. I'm gonna try this out.

June 9, 2008 9:50 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About kemaltolga

Born in Ankara, Turkey. Raised in Germany. Recruited for the Track & Field Team at Texas A&M University in College Station, TX, in January 2000. NCAA champion in 2001 (Discus). Work for design firm in southern California. Married with two daughters.