Add a Sharepoint solution programmatically
You can add a sharepoint solution file by stsadm.exe however sometime you can need to add a solution programmatically in the solution store.
Microsoft.SharePoint.Administration namespace contains many methos to add solution, Remove solution and download solution etc. The first sampe demostrate how to add a wsp solution file to solution strore without stsadm - o addsolution command. To to this I just created a new console application and add Microsoft.SharePoint.dll assembly on my console application.
SPSolution mySolution =
SPFarm.Local.Solutions.Add(@"c:\CMS.SharePoint.CFT_RegEx.wsp");
After run this console application the solution file add programmaticlly to solution store on your SharePoint.
This example only add solution to solution store but we need addsolution and deploy it programmatically. To do this we need to usee SPSolution.Deploy medhod.
SPSolution mySolution =
SPFarm.Local.Solutions.Add(@"c:\CMS.SharePoint.CFT_RegEx.wsp");
Collection<SPWebApplication> webapps = new Collection<SPWebApplication>();
SPWebApplication webapp = SPWebApplication.Lookup(new Uri("http://testsite"));
webapps.Add(webapp);
mySolution.Deploy(DateTime.Now, false, webapps, false);
Finnally your solution add and deployed on Sharepoint.
SPSolutionCollection and Working with Solutions - I
SPSolutionCollection class belong to Microsoft.SharePoint.Administration NamesSpace in the Microsoft.SharePoint.dll assembly. This class contains many funcinalit methods to manage your solutions. The example show us how to displaye solutions which installed your local farm.
SPSolutionCollection myColl = SPFarm.Local.Solutions;
foreach (SPSolution mysolution in myColl)
{
Console.WriteLine("Solution Name : {0}, Deployed :{1}, Status {2}",
mysolution.DisplayName, mysolution.Deployed.ToString(),
mysolution.Status.ToString());
}
First line create an object contains a collection of solution in the local farm. Foreach statements loop each solutions and display their name and some properties.