Deploy a SharePoint Solution Programmatically

A lot of you are familiar with the new solutions story within Windows SharePoint Services 3.0. In short, you package your solution components (assemblies, features, …) together with a manifest file into a SharePoint solution file (represented by a wsp file). Next, you can add the SharePoint solution to the solution store (typically using stsadm) and the using the SharePoint 3.0 Central Administration, you can deploy it to one of the available site collections either locally or on front-end Web Servers or application servers.

All of this is also exposed in the object model. I am currently finishing an MSDN article titled 'SharePoint Development Tools and Techniques for Creating, Packaging, Deploying, and Maintaining Code' and I have included some discussions on all of this. As a small teaser, here are the steps how you can add a solution to a solution store and deploy it to a site collection using C# code assuming you deploy everything on a local machine.

// -- adding the solution to the solution store
SPSolution solution = SPFarm.Local.Solutions.Add(@"C:\Packages\HelloWebPart.wsp");

// -- deploying the solution to a site collection
Collection<SPWebApplication> webapps = new Collection<SPWebApplication>();
SPWebApplication webapp = SPWebApplication.Lookup(new Uri(""));
solution.DeployLocal(true, webapps, true);

The assembly you need to reference is the Microsoft.SharePoint.dll and the namespaces used here are Microsoft.SharePoint.Administration and System.Collections.ObjectModel.

Have fun!