Deploy a solution to the sandbox in C#

I came across a question on StackOverflow about how to programmatically deploy a sandbox solution, and this seemed a good question. Here’s what I came up with…

SPSite site = ...
SPFile sourceSolutionFile = ...
//Get the Solution Gallery for the SPSite
SPDocumentLibrary solutionGallery = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);
//Add the WSP File. I've used a source that is an SPFile, but really it's a string and byte array
SPFile solutionFile = solutionGallery.RootFolder.Files.Add(sourceSolutionFile.File.Name, sourceSolutionFile.File.OpenBinary());
// Activate Solution
SPUserSolution newUserSolution = newSite.Solutions.Add(solutionFile.Item.ID);

Edit: Additional note – you can use this to copy Site Templates (as WSPs) from one Site Collection to another.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>