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!


All SharePoint Versions

The web parts are functional components that extend your SharePoint environment whether it’s hosted, on-premises, or part of Microsoft® Office 365.

SharePoint 2013, 2016, 2019, Online (Office 365)

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2013, 2016, 2019

SharePoint

Product Suites

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


Essentials Suite


Essentials Plus Suite


Bamboo Premier Suite


Project Management Suite


Knowledge Management Suite


External User Manager


 

For more information on our product suites, contact us.

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!