Exporting existing wsp packages from SharePoint

by Martin 5. juli 2010 13:01

I'm currently working on a project with migrating from one SharePoint installation to another newer installation. For this project, the client has a couple of installed wsp's in the farm. But they do not have the wsp's to reinstall them... So I somehow had to write up some code that could export a given wsp from the farm. I ended up writing an extension for STSADM (yeah, I know, I should use PowerShell, but I'm an oldie for a little longer :-)). I thought, I'd share the end result with those who wanted to give it a go. (Use the supplied wsp at your own risk)

The source code goes here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using System.Collections.Specialized;
using Microsoft.SharePoint.Administration;

namespace ExportWSP
{
	public class ExportWSP : ISPStsadmCommand
	{
		public string GetHelpMessage(string command)
		{
			return "-wsp ";
		}

		public int Run(string command, StringDictionary keyValues, out string output)
		{
			if (!keyValues.ContainsKey("wsp") && !keyValues.ContainsKey("all"))
			{
				throw new InvalidOperationException(string.Format("USAGE: -eit-exportwsp -wsp  [-all]. You supplied wsp: {0}, all: {1}", keyValues["wsp"], keyValues["all"]));
			}

			try
			{
				this.ExportWSPPackage(keyValues);
			}
			catch (Exception ex)
			{
				output = ex.Message;
				return 1;
			}

			output = "Success";
			return 0;
		}

		private void ExportWSPPackage(StringDictionary keys)
		{
			if (keys.ContainsKey("all"))
			{
				foreach (SPSolution sol in SPFarm.Local.Solutions)
				{
					this.ExportWSPPackage(sol.Name);
				}
			}
			else
			{
				this.ExportWSPPackage(keys["wsp"]);
			}
		}

		private void ExportWSPPackage(string wspName)
		{
			//Get reference to solution
			SPSolution sol = SPFarm.Local.Solutions[wspName];

			if (sol != null)
			{
				//Save file (assuming it does so to the execution path
				sol.SolutionFile.SaveAs(wspName);
			}
			else
			{
				throw new InvalidOperationException("Solution does not seem to exist");
			}
		}
	}
}

Below is a link to the wsp-file. The project has been built with STSDev a tool I absolutely love when developing SharePoint solutions!

To use the extension, simply use one of the following commands:

stsadm -o eit-exportwsp -all , or
stsadm -o eit-exportwsp -wsp <NAME OF WSP TO EXPORT>

The first line exports all the wsp's in the farm to the folder the command is executed from. The second export the given wsp to the folder the command is executed from.

Parameters explained:
-all: Exports ALL WSP's in the farm
-wsp <NAME OF WSP TO EXPORT>: exports a single wsp from the farm.

To install the solution, simply use:

stsadm -o addsolution -f exportwsp.wsp
stsadm -o deploysolution -n ExportWSP.wsp -imm -allowgac
stsadm -o execadmsvcjobs

 

ExportWSP.wsp (8,21 kb)

Tags: , , , ,

.NET | MOSS

Tilføj kommentar

  Country flag

biuquote
  • Kommentar
  • Eksempel
Loading

Page List

About

Take a look at my LinkedIn-profile.

DISCALIMER: The content on this blog is in NO way affiliated with my employer. The opinions expressed on this blog are solely my own and in NO way endorsed by my employer.

Recent comments

Comment RSS

Month List