SharePoint Use Cases

  • Home
  • About
  • Our Tools
  • Contact me
SharePoint 2010

Compile and package Sandboxed and Farm solutions using the same code base

Toni Frankola - July 4, 2010

SharePoint Sandboxed solutions allow site administrators to deploy custom web parts without physical (console) access to the SharePoint server. Sandboxed solutions are easily uploaded via Solutions gallery.

Sometimes, while developing a solution, you want to know if your code is running sandboxed or as a normal farm solution. In order to check if the code is running sandboxed or not, you can use the approaches suggested to me by Wictor Wilen and Chris O’Brien.

I recently installed Visual Studio 2010 SharePoint Power Tools (I highly recommended you to install these too). This pack introduces a new ability to check if a sandboxed solution contains any code which will not be able to run sandboxed on a SharePoint farm. (Check this article for more details on SharePoint sandboxed solutions restrictions).

The ability to check if you are using any prohibited namespaces or functions at compilation time ensures that you do not deploy a sandboxed solution that will not work as advertised. However, in case you want to package both Sandboxed and Farm solutions from the same (or similar) code base, there is a simple trick, and this article explains how to it. The typical scenario for something like this is when you have a web part that works as Sandboxed solution but in case your code is deployed as Farm solution you want to use some additional functionalities that are not available while running in a Sandbox. In my case I created Search AutoComplete for SharePoint 2010 Central Administration Web Part, by default it works as Sandboxed Solution but it can only list Service Apps links if deployed as Farm solution.

Here is what you need to do in order to compile both solutions from the same code base:

  • Create two new build configurations for your project Debug-Sandbox and Release-Sandbox
  • Define new Conditional Compilation Symbols: DebugSandbox for Debug-Sandbox configuration and ReleaseSandbox for Release-Sandbox configuration
  • Open your project file (csproj or vbproj extension) in Notepad
  • Locate Project Group tags and make sure you have 4 project groups and SandboxedSolution node in each Project Group node. This node determines if solution is going to be complied as sandboxed or farm solution. Set the value to TRUE for your sandboxed build configurations and FALSE for Debug and Release. (As shown on picture below)

To separate your code preprocessor directive needs to be used, as in this example:

protected override void Render(HtmlTextWriter writer)
        {

#if DebugSandbox || ReleaseSandbox
            //This code block will compile if sandboxed build configuration (Debug-SandBox or Release-Sandbox) is chosen
           writer.Write("Hello world, I am a sandboxed web part!<br/>");
           writer.Write("This web part is sandboxed solution and cannot retrieve total number of sandboxed solutions.");
#else
            //This code block will compile if non sandboxed build configuration is chosen
           writer.Write("Hello world, I am NOT a sandboxed web part!<br/>");

            SPUserSolutionCollection solutions = SPContext.Current.Site.Solutions;
            writer.Write(string.Format("There is a total of <b>{0}</b> sandboxed solutions installed in this site collection!<br/><br/>", solutions.Count));

            if (solutions.Count > 0)
            {
                writer.Write("These are the installed solutions:<br/><ul>");
                foreach (SPUserSolution solution in solutions)
                {
                    writer.Write(string.Format("<li>{0}</li>", solution.Name));
                }
                writer.Write("</ul>");
            }
#endif

            base.Render(writer);

        }

When you compile this example as a farm solution it will display the list of installed sandboxed solutions. When compiled as sandboxed solutions it will display a message text only.

Conclusion

This technique might come handy when you have two web parts that have only few differences and you do not want to maintain a separate code base just for that. The down side of this approach is that you cannot have both build configurations deployed to the same site collection at the same time because both share the same feature id.

Tags | sandbox-solutions, sandboxed-solutions, SharePoint, sharepoint 2010
 0
Share Now

Toni Frankola

Entrepreneur, IT consultant, speaker, blogger, and geek. Co-founder of syskit.com.

You Might Also Like

SharePoint, SharePoint 2010

SharePoint and Remote Blob Storage (RBS) questions

December 22, 2010
SharePoint, SharePoint 2010

Retract SharePoint solution from Central Admin with PowerShell

July 7, 2010
SharePoint, SharePoint 2010

Add all site collections as PerformancePoint trusted locations via PowerShell

August 4, 2010
SharePoint, SharePoint 2010

The SharePoint50 Project: Who are the main influencers in SharePoint?

September 27, 2010
Previous Post Remove all Service Applications from SharePoint 2010 farm with PowerShell
Next Post Retract SharePoint solution from Central Admin with PowerShell

Archives

  • December 2021 (1)
  • June 2021 (1)
  • January 2021 (1)
  • July 2019 (1)
  • June 2019 (2)
  • March 2019 (1)
  • February 2019 (3)
  • October 2018 (2)
  • August 2018 (2)
  • June 2018 (1)
  • April 2018 (1)
  • February 2018 (1)
  • January 2018 (1)
  • June 2017 (1)
  • April 2017 (1)
  • February 2017 (1)
  • November 2016 (2)
  • August 2016 (1)
  • July 2016 (2)
  • March 2016 (1)
  • February 2016 (2)
  • January 2016 (1)
  • November 2015 (2)
  • December 2014 (1)
  • November 2014 (2)
  • July 2014 (2)
  • June 2014 (2)
  • May 2014 (1)
  • April 2014 (2)
  • March 2014 (1)
  • February 2014 (4)
  • January 2014 (2)
  • December 2013 (1)
  • September 2013 (1)
  • July 2013 (1)
  • June 2013 (1)
  • May 2013 (1)
  • April 2013 (3)
  • February 2013 (1)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (1)
  • August 2012 (1)
  • July 2012 (2)
  • June 2012 (4)
  • April 2012 (1)
  • February 2012 (1)
  • January 2012 (1)
  • November 2011 (2)
  • October 2011 (2)
  • September 2011 (2)
  • July 2011 (3)
  • May 2011 (2)
  • April 2011 (1)
  • March 2011 (4)
  • February 2011 (3)
  • January 2011 (4)
  • December 2010 (4)
  • November 2010 (3)
  • October 2010 (3)
  • September 2010 (4)
  • August 2010 (1)
  • July 2010 (4)
  • May 2010 (2)
  • April 2010 (2)
  • March 2010 (3)
  • January 2010 (2)
  • December 2009 (3)
  • November 2009 (5)
  • October 2009 (2)
  • August 2009 (7)
  • July 2009 (3)
  • June 2009 (4)
  • May 2009 (3)
  • April 2009 (5)
  • March 2009 (8)
  • February 2009 (5)
  • January 2009 (8)
  • December 2008 (9)
  • November 2008 (8)
  • October 2008 (7)
  • September 2008 (4)
  • August 2008 (6)
  • July 2008 (5)
  • June 2008 (3)
  • May 2008 (4)
  • April 2008 (6)
  • March 2008 (4)
  • February 2008 (3)
  • January 2008 (6)
  • December 2007 (1)
  • November 2007 (3)
  • October 2007 (3)
  • September 2007 (2)
  • August 2007 (1)
  • July 2007 (12)
  • June 2007 (2)
  • May 2007 (3)

Connect Us

All postings on this blog are provided “AS IS” with no warranties, and confer no rights. All entries in this blog are my opinion and don’t necessarily reflect the opinion of my employer.

  • Home
  • About
  • Our Tools
  • Contact me

Copyright (c) Toni Frankola 2008. - 2019.