Download wsp solution file from SharePoint Farm

Some times you need to download the solution file from SharePoint Farm. SharePoint user interface does not provide any functionality to achieve it .  Don’t worry, you can extract the solution file from SharePoint Farm by using object model. Here is a simple function to extract your target solution file from SharePoint.

1/// <summary>
2/// Downloads the solution file from SharePoint Farm.
3/// </summary>
4/// <param name="targetSolutionName">Name of the target solution.</param>
5/// <param name="pathToSave">The path to save.</param>
6private static void DownloadSolutionFile(string targetSolutionName, string pathToSave)
7{
8    if (!String.IsNullOrEmpty(targetSolutionName) &amp;&amp; !String.IsNullOrEmpty(pathToSave))
9    {
10        //Search for target solution
11        var solution = (from s in SPFarm.Local.Solutions where s.Name == targetSolutionName select s).FirstOrDefault();
12 
13        if (solution != null)
14        {
15            //target solution found, extract the solution file and save it
16            SPPersistedFile solutionFile = solution.SolutionFile;
17            solutionFile.SaveAs(pathToSave + solutionFile.Name);
18        }
19    }
20}
Share