Freigeben über


Presentation + kod från sommarkollo: Sharepoint och Silverlight

Idag gästspelade jag på Pontus Haglunds sommarkollo-semiarie "Sharepoint för utvecklare" och visade hur lätt det är att integrera Silverlight i Sharepoint.

Mina slides finns här (jag är förstås grymt avundsjuk på Pontus som körde sina slides med pptPlex).

Jag visade dels hur du kan lägga till en Sharepoint-webpart för att kunna använda Vertigos underbara Slide.Show Silverlight 1.0-bildvisare (as featured on MSDN.se!).

Koden för min webpart nedan:

 

 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Web.UI;
 using System.Web.UI.WebControls.WebParts;
 using System.ComponentModel;
 using System.Text;
  
 namespace SLDemo1
 {
     [Guid("02b3a17c-b19e-42de-bafa-eb5da4268c23")]
     public class SLDemo1Webpart : Microsoft.SharePoint.WebPartPages.WebPart
     {
         private bool error = false;
  
         private string silverlightJSPath = "/_layouts/Silverlight/";
         private string slideShowJS = "SlideShow/SlideShow.js";
  
         
         [Browsable(true),
         Personalizable(), WebBrowsable,
         Category("Custom Settings"),
         DefaultValue("/_layouts/Silverlight/"),
         Description("Silverlight JS path - ex:/_layouts/Silverlight/")]
         public string SilverlightJSPath
         {
             get
             {
                 return silverlightJSPath;
             }
  
             set
             {
                 silverlightJSPath = value;
             }
         }
  
         [Browsable(true),
         Personalizable(), WebBrowsable,
         Category("Custom Settings"),
         DefaultValue("SlideShow/SlideShow.js"),
         Description("SlideShow js subpath + name - ex: SlideShow/SlideShow.js")]
         public string SlideShowJS
         {
             get
             {
                 return slideShowJS;
             }
  
             set
             {
                 slideShowJS = value;
             }
         }
  
         public SLDemo1Webpart()
         {
             this.ExportMode = WebPartExportMode.All;
         }
  
         /// <summary>
         /// Create all your controls here for rendering.
         /// Try to avoid using the RenderWebPart() method.
         /// </summary>
         protected override void CreateChildControls()
         {
             if (!error)
             {
                 try
                 {
  
                     base.CreateChildControls();
                     StringBuilder sb = new StringBuilder();
                     sb.Append("<div id='SilverlightControlHost" + this.ClientID + "' Class=''>");
                     sb.Append("<script type=\"text/javascript\"> ");
                     sb.Append("new SlideShow.Control(new SlideShow.XmlConfigProvider());");
                     sb.Append("</script></div>");
  
                     // Your code here...
                     this.Controls.Add(new LiteralControl(sb.ToString()));
                 }
                 catch (Exception ex)
                 {
                     HandleException(ex);
                 }
             }
         }
  
  
  
         protected override void OnPreRender(EventArgs e)
         {
  
             String jsString;
             jsString = "<script type=\"text/javascript\" src=\"" + silverlightJSPath + slideShowJS + "\"></script>\n";
  
             if (!Page.ClientScript.IsClientScriptBlockRegistered("SlideShowJS"))
                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SlideShowJS", jsString);
  
             jsString = "<script type=\"text/javascript\" src=\"" + silverlightJSPath + "Silverlight.js\"></script>\n";
  
             if (!Page.ClientScript.IsClientScriptBlockRegistered("SilverlightJS"))
                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SilverlightJS", jsString);
         }
  
         /// <summary>
         /// Ensures that the CreateChildControls() is called before events.
         /// Use CreateChildControls() to create your controls.
         /// </summary>
         /// <param name="e"></param>
         protected override void OnLoad(EventArgs e)
         {
             if (!error)
             {
                 try
                 {
                     base.OnLoad(e);
                     this.EnsureChildControls();
  
                     // Your code here...
                 }
                 catch (Exception ex)
                 {
                     HandleException(ex);
                 }
             }
         }
  
         /// <summary>
         /// Clear all child controls and add an error message for display.
         /// </summary>
         /// <param name="ex"></param>
         private void HandleException(Exception ex)
         {
             this.error = true;
             this.Controls.Clear();
             this.Controls.Add(new LiteralControl(ex.Message));
         }
     }
 }

 

Jag visade även hur du kan bygga en generisk Sharepoint Webpart som kan rendera en Silverlight 2-applikation utifrån valfri .XAP-fil som du refererar till (förutsatt förstås att Silverlight-applikationen "står på egna ben" som Pontus så galant översatte "self contained" till).

Här är koden för min generiska Silverlight 2-webpart:

 

 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Web.UI;
 using System.Web.UI.WebControls.WebParts;
 using System.Text;
  
 namespace GenericXAPWebPart
 {
     [Guid("27ba16c8-ae2e-4e21-b43f-316f0518d667")]
     public class GenericXAPWebPart : Microsoft.SharePoint.WebPartPages.WebPart
     {
         private bool error = false;
         private string xap = null;
  
         [Personalizable(PersonalizationScope.Shared)]
         [WebBrowsable(true)]
         [System.ComponentModel.Category("Custom Settings")]
         [WebDisplayName("XAP-fil")]
         [WebDescription("Sökväg och namn på XAP-fil")]
         public string XAP
         {
             get
             {
                 if (xap == null)
                 {
                     xap = "error";
                 }
                 return xap;
             }
             set { xap = value; }
         }
  
  
         public GenericXAPWebPart()
         {
             this.ExportMode = WebPartExportMode.All;
         }
  
         /// <summary>
         /// Create all your controls here for rendering.
         /// Try to avoid using the RenderWebPart() method.
         /// </summary>
         protected override void CreateChildControls()
         {
             if (!error)
             {
                 try
                 {
                     base.CreateChildControls();
                     StringBuilder sb = new StringBuilder();
  
                     sb.Append("<object style='width: 100%; height: 100%' type='application/x-silverlight'>');
                     sb.Append("<param name='source' value='" + xap + "' />");
                     sb.Append("<param name='version' value='2.0' />");
                     sb.Append("<param name='enableHtmlAccess' value='true' />");
                     sb.Append("</object>");
  
                     this.Controls.Add(new LiteralControl(sb.ToString()));
                 }
                 catch (Exception ex)
                 {
                     HandleException(ex);
                 }
             }
         }
  
         /// <summary>
         /// Ensures that the CreateChildControls() is called before events.
         /// Use CreateChildControls() to create your controls.
         /// </summary>
         /// <param name="e"></param>
         protected override void OnLoad(EventArgs e)
         {
             if (!error)
             {
                 try
                 {
                     base.OnLoad(e);
                     this.EnsureChildControls();
  
                     // Your code here...
                 }
                 catch (Exception ex)
                 {
                     HandleException(ex);
                 }
             }
         }
  
         /// <summary>
         /// Clear all child controls and add an error message for display.
         /// </summary>
         /// <param name="ex"></param>
         private void HandleException(Exception ex)
         {
             this.error = true;
             this.Controls.Clear();
             this.Controls.Add(new LiteralControl(ex.Message));
         }
     }
 }