Pages

Search This Blog

Wednesday, January 11, 2012

Download WSP Solution from SharePoint


Once a solution has been added to SharePoint, then there's no way to download it. Using the SharePoint object model we can easily download a WSP using the 'SaveAs' method. 
The C# code below illustrates how to download all of the solutions in the farm with just a few lines.

private void DownLoadAllSolutions()
       {
            SPSolutionCollection wspSolutions = SPFarm.Local.Solutions;
            foreach (SPSolution solution in wspSolutions)
            {
SPPersistedFile wspFile = solution.SolutionFile;
DownLoadWSP(wspFile);
            }
        }
        private void DownLoadWSP(SPPersistedFile wspFile)
{
    if (wspFile != null)
    {
string wspFileName = Path.Combine(@"c:\temp",wspFile.Name);
                if(File.Exists(wspFileName))
{
File.Delete(wspFileName);
}
wspFile.SaveAs(wspFileName);
}
}

No comments:

Post a Comment