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();
}
}
}
}