Download SharePoint 2010 Solutions

I was working on a migration between two SharePoint 2010 farms yesterday and I couldn’t locate the WSP files for some of the solutions actually on the file system, so I wanted to download the WSP files from the source farm to upload into the target farm.  Now there is no way to actually do this OOTB, which I’ve always throught strange … however I did see this post from Djamel Chagour.  It was however for SharePoint 2007 and written in VB.NET.  Now unlike some people I could mention, I don’t give a monkey’s what language is used to write an application, VB.NET is just fine by me. It all ends up as ones and zeros in the end.  However since it’s a little routine which we at Ridgian will probably want to reuse and improve upon, I thought I’d refactor it into C#, to stop some of the developer’s giving me grief about it being in VB.NET!  I’ve also changed the script it generates to be PowerShell rather than STSADM which as everybody should know by now is deprecated …

So here’s the code:

using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace Ridgian.SP.DownloadSolutions
{
  class Program
  {
    static void Main(string[] args)
    {
extWriter textWriter = null;

try
{

SPSolutionCollection solutions = SPFarm.Local.Solutions;
String _path = "SolutionsToDeploy/";
Boolean _allowGac = false;
Boolean _allowCas = false;

// Create the folder if it does not exist
if (!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}

// Create a file stream where StsAdm command will be added
textWriter = new StreamWriter(_path + "DeploySolutions.ps1");
foreach (SPSolution solution in solutions)
{
// See if the parameters -GACDeployment and -CASPolicies are needed
_allowGac = solution.ContainsGlobalAssembly ? true : false;
_allowCas = solution.ContainsCasPolicy ? true : false;
String text = String.Empty;

// Extract the solution into a file
SPPersistedFile wsp = solution.SolutionFile;
wsp.SaveAs(_path + solution.Name);

// For every solution we need two commands : Add-SPSolution and Install-SPSolution
text = @"Add-SPSolution -LiteralPath """ + _path + solution.Name + @"""";
textWriter.WriteLine(text);
if (solution.ContainsWebApplicationResource && solution.DeployedWebApplications.Count() > 0)
{
foreach (SPWebApplication wapp in solution.DeployedWebApplications)
{
text = @"Install-SPSolution -Identity """ + solution.Name + @""" -WebApplication """ + wapp.GetResponseUri(SPUrlZone.Default).ToString() + (_allowGac ? @""" -GACDeployment" : "") + (_allowCas ? @" -CASPolicies" : "") + @" -Local";
}
}
else
{
text = @"Install-SPSolution -Identity """ + solution.Name + @"""" + (_allowGac ? @""" -GACDeployment" : "") + (_allowCas ? @" -CASPolicies" : "") + @" -Local";
}
textWriter.WriteLine(text);

// Execute the timer job
text = "Start-SPAdminJob";
textWriter.WriteLine(text);
textWriter.WriteLine();
}
// Write the command file to disk and close the stream
textWriter.Flush();
}
  catch (Exception ex)
{
  Console.WriteLine(ex.Message);
  Console.ReadLine();
}
finally
{
  textWriter.Close();
}
}
}
}

My thanks to  Djamel and hopefully this will help somebody somewhere!

Cheers

Dave Mc

Advertisements

About davemcmahon81
Software Developer & Architect, User Group Leader, Speaker, Writer, Blogger, Occasional Guitarist, Man-made Global Warming Sceptic, Climate Change Believer, General Optimist but most of all proud Husband and Dad ...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: