Using Common DLLs in Sandboxed Solutions

Office 365 brings a wealth of new possibilities to the development community.  In a recent article, Hong Kong alone saw a 250% increase in new customers since the Office 365launch in June last year. For the SharePoint community, Office 365 provides the “SharePoint Online” program built upon SharePoint 2010 technology.

One of the many capabilities of the SharePoint 2010 technology is the ability to create visual components called Web Parts to display various bits of information. SharePoint Online provides this capability, although in a limited capacity called “Sandbox Solutions.” For more information about Sandbox Solutions and their capabilities (and limitations) see Sandbox Solution Considerations MSDN, and Understanding the SharePoint 2010 Sandbox Limitations Wictor Wilen. 

As in any Web Part solution, it is built upon a collection of files and assemblies. In this article, I will discuss how you can create a Common Assembly that you can reuse in all of your solutions.  NOTE: Although “common assembly” commonly means you can have just one instance of the assembly and reference it from many projects, one of the limitations of Sandbox Solutions is that they cannot reference another solution. This instead allows you to pre-build an assembly and quickly include it in multiple projects, saving valuable time. 

So, how do you reference a common assembly in your package? I will use a simple example where the assembly outputs a Namespace text in a Web Part. In this article, I am using Visual Studio 2010 (Ultimate) on a machine where SharePoint is installed. 

To start, you need to first create a Visual Studio solution similar to what is shown below:

The project Unknown.CommonLibrary is currently an empty SharePoint 2010 Project that will store all our common codes.

The project Unknown.HelloSharePointOnline is also an empty SharePoint 2010 Project that will display the results of the Unknown.CommonLibrary results in a Web Part. 

I then create the file UnknownConstants.cs within the Unknown.CommonLibrary project: 

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace Unknown.CommonLibrary
   7: {
   8:     public class UnknownConstants
   9:     {
  10:         private const string UNKNOWN_NAMESPACE = "Unknown.CommonLibrary";
  11:  
  12:         public UnknownConstants()
  13:         {
  14:         }
  15:  
  16:         public static string GetCommonNamespace()
  17:         {
  18:             return UNKNOWN_NAMESPACE;
  19:         }        
  20:     }
  21: }

Within the Unknown.HelloSharePointOnline project, I also created a file called HelloWebPart.cs:

   1: using System;
   2: using System.ComponentModel;
   3: using System.Web;
   4: using System.Web.UI;
   5: using System.Web.UI.WebControls;
   6: using System.Web.UI.WebControls.WebParts;
   7: using Microsoft.SharePoint;
   8: using Microsoft.SharePoint.WebControls;
   9:  
  10: namespace Unknown.HelloSharePointOnline.HelloWebPart
  11: {
  12:     [ToolboxItemAttribute(false)]
  13:     public class HelloWebPart : WebPart
  14:     {
  15:         protected override void CreateChildControls()
  16:         {
  17:             string strHello = string.Format("Welcome {0} to SharePoint Online", Unknown.CommonLibrary.UnknownConstants.GetCommonNamespace());
  18:             this.Controls.Add(new LiteralControl(strHello));
  19:         }
  20:     }
  21: }

Now within the project Unknown.HelloSharePointOnline, click on the node Package and select the Advanced tab at the bottom of the panel:

Click Add and choose “Add Assembly from Project Output…”:

Select “Unknown.CommonLibrary” with Safe Controls as shown below:

After that, right click at Unknown.HelloSharePointOnline project and select Package:

Your final output will be a *.wsp file. Open your Solutions Gallery in SharePoint Online and upload your new solution. Add your new HellowWebPart solution to the page and you will see it output the following:

I hope this article is helpful as you’re developing for SharePoint Online!