Monday 9 July 2012

Add,Deploy and retract Sharepoint solutions programmatically


Deploy and retract Sharepoint solutions programmatically

The below is the sample code for the adding the Sharepoint solution to the solutionmanagement store and deploying the solution locally.

try
{
SPSolution solution = SPFarm.Local.Solutions.Add(wspOutputFile);
Collection selectedWebApps = new Collection();
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://localhost"));
selectedWebApps.Add(webApp);

solution.DeployLocal(true, true);
}
catch{}

You need to add the assemblies Microsoft.SharePoint.Administration and System.Collections.ObjectModel at the beginning of the code. To deploy the solution to other web applications, you can add them each after the selectedWebApps.Add(webApp); step.


The below is the code to retract the solution from the solution management store.

try
{
SPSolution solution = SPFarm.Local.Solutions[wspName];
solution.RetractLocal();
SPFarm.Local.Solutions.Remove(wspName);
}
catch{}

In the above code, wspName variable contains the name of your WSP solution with the extension.

Happy Coding!

1 comment:

  1. Can i Add the solution from Document Library instead of Local path ?

    ReplyDelete