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.

Deploy a solution to the sandbox in C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.