Extract the installed solutions from the MOSS farm (updated)

I was looking today for a way to get all the original wsp and cab files that have been installed into a MOSS farm. Of course not everybody keeps track of where the original files are the same way. Certainly if there is a team that is managing the same MOSS farms, your original files get lost very rapidly, or you can find the files on the filesystem, but there are so many versions of them. which one is the latest and the version installed?

So I was thinking that there should be a way to extract them from MOSS directly. Of course there is no OOTB way to do this, but I found some code that allows you to do it. The piece of code was posted on a Technet blog comment

I basically took this code and wrote a small Console Application around it so I could use it on my systems.

*** UPDATE March 23rd 2009***

Once I had created my little tool to extract the solutions, I noticed that it would also be great to know what the properties are of the solutions. Where were they deployed to, etc. Therefore I added some code to generate in addition to the solution files also a corresponding xml file containing that information

I will share my updated code:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint.Administration;

using System.Xml;

 

namespace SolutionExtractor

{

    class Program

    {

        static void Main(string[] args)

        {

            if (args.GetLength(0) == 0)

            {

                Console.WriteLine(“Usage: SolutionExtracter.exe <path to save solution files>”);

                return;

            }

            string path = args[0].ToString();

           

            bool checkIfDirExists = System.IO.Directory.Exists(path);

            if (checkIfDirExists == false)

            {

                Console.WriteLine(“path “+path+ ” does not exist”);

                return;

            }

 

            SPSolutionCollection solutions = SPFarm.Local.Solutions;

 

            foreach (SPSolution solution in solutions)

            {

                SPPersistedFile wspFile = solution.SolutionFile;

                Console.WriteLine(“Saving “+path +“\\” + solution.Name);

                wspFile.SaveAs(path +“\\” + solution.Name);

               

                Console.WriteLine(“Saving properties to “ + path + “\\” + solution.Name + “.xml”);

                string filename = path + “\\” + solution.Name + “.xml”;

                XmlTextWriter tw =new XmlTextWriter(filename,null);//null represents the Encoding Type//

                tw.Formatting=Formatting.Indented;  //for xml tags to be indented//

                tw.WriteStartDocument();   //Indicates the starting of document (Required)//

                tw.WriteStartElement(“Solution”);

                tw.WriteAttributeString(“Name”, solution.Name);

                tw.WriteStartElement(“Properties”);

                tw.WriteElementString(“Added”, solution.Added.ToString());

                tw.WriteElementString(“ContainsCasPolicy”, solution.ContainsCasPolicy.ToString());

                tw.WriteElementString(“ContainsGlobalAssembly”, solution.ContainsGlobalAssembly.ToString());

                tw.WriteElementString(“ContainsWebApplicationResource”, solution.ContainsWebApplicationResource.ToString());

                tw.WriteElementString(“Deployed”, solution.Deployed.ToString());

                

                tw.WriteStartElement(“DeployedServers”);

                foreach(SPServer s in solution.DeployedServers)

                    tw.WriteElementString(“Server”, s.Name);

                tw.WriteEndElement();

               

                tw.WriteElementString(“DeploymentState”, solution.DeploymentState.ToString());

                if (solution.DeploymentState.ToString().ToLower() != “globaldeployed”)

                {

                    tw.WriteStartElement(“DeployedWebApplications”);

                    foreach (SPWebApplication w in solution.DeployedWebApplications)

                        tw.WriteElementString(“WebApplication”, w.Name);

                    tw.WriteEndElement();

                }

                tw.WriteElementString(“IsWebPartPackage”, solution.IsWebPartPackage.ToString());

                tw.WriteElementString(“SolutionId”, solution.SolutionId.ToString());

              

                tw.WriteEndElement();

                tw.WriteEndDocument();                                 

                tw.Flush();

                tw.Close();

            }

 

        }

    }

}

 

 

 

So after running this code you will find all the solutions accompanied by an xml file with the properties like this:

 <?xml version=1.0?>
<
Solution Name=mySolution.wsp>
  <
Properties>
    <
Added>True</Added>
    <
ContainsCasPolicy>False</ContainsCasPolicy>
    <
ContainsGlobalAssembly>True</ContainsGlobalAssembly>
    <
ContainsWebApplicationResource>True</ContainsWebApplicationResource>
    <
Deployed>True</Deployed>
    <
DeployedServers>
        <
Server>MOSS01</Server>
        <Server>MOSS02</Server>
    </
DeployedServers>
    <
DeploymentState>GlobalAndWebApplicationDeployed</DeploymentState>
    <DeployedWebApplications>
       <
WebApplication>SharePoint – WebApp1</WebApplication>
       <
WebApplication>SharePoint – WebApp2</WebApplication>
    </
DeployedWebApplications>
    <
IsWebPartPackage>False</IsWebPartPackage>
    <
SolutionId>eb07011e-3898-4d51-8e23-008a890c6549</SolutionId>
  </
Properties>
</
Solution>

For Those of you that do not want to start developping or compiling this peace of code, you can download an updated compressed version of it attached to this post.

2 thoughts on “Extract the installed solutions from the MOSS farm (updated)

  1. Thanks for this. I was actually looking for some code that showed how to access them as files, but it turns out that your little number already does exactly what I was trying to do so you saved me some time too. rock and roll.

Leave a Reply

Your email address will not be published. Required fields are marked *