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.