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!


SharePoint

Applications

SharePoint apps are stand-alone applications that perform specific tasks on a SharePoint site. Apps can perform functions such as managing a discussion board or knowledge base, performing project management or time tracking tasks, or doing other workflow operations.

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials


Essentials Plus


Bamboo Premier


Project Management Suite


Knowledge Management Suite


External User Manager


SharePoint

Web Parts

Extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of Web Parts. Web Parts are the building blocks of pages on a SharePoint site that can be used to customize the user interface and content of a site page. 

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials


Essentials Plus


Bamboo Premier


Project Management Suite


Knowledge Management Suite


External User Manager


Office 365

Cloud Parts

Cloud Parts are functional components that extend your SharePoint environment whether it’s hosted, on-premises, or part of Microsoft Office 365. More than mere ports of existing software to the cloud, our Cloud Parts have been built from the ground up to take advantage of the best that the cloud has to offer.

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Cloud Parts Suite for O365/SP Onl.


Featured Services

SharePoint Health Check

A SharePoint Health Check will identify the causes of issues and risks associated with your specific environment, and is custom tailored to provide you with the best recommendations to optimize your SharePoint environment.

SQL Health Check

Document recommendations relating to performance, stability, availability, or a specific focus you request of your SQL Server database instances.

My SharePointXperts

The truth is that each SharePoint skill may not be a full time job for many organizations, and it is nearly impossible for one person to do everything you need – so augment your team with SharePointXperts; providing the skill sets you need when you need them!