Online Training On SharePoint
                      

Saturday 26 February 2011

Retriving SharePoint WSP Files from Config DB using SharePoint Object Model and PowerShell

WSP files are the preferred way of deploying the new functionlaity in SharePoint. WSP files are stored in the Config DB. If you need to extract these files you can do that by using the SharePoint Object Model Code. The following code snippet will retrive all the solutions from the config DB and will place them in the specified location:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Administration; 

namespace Retrive_Solutions 
{  
    class SolutionBackup 
    { 
        static void Main(string[] args) 
        { 
            SPSolutionCollection AllSolutions = SPFarm.Local.Solutions; 
            foreach (SPSolution Solution in AllSolutions) 
            { 
                SPPersistedFile wspFile = Solution.SolutionFile; 
                wspFile.SaveAs("C:\\Solutions\\" + Solution.Name); 
            } 
        } 
    } 
}

The same also can be achieved using PowerShell so no need to write any OM code. Here is the PowerShell script to acheive the same:

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local 

$path = Split-Path $MyInvocation.MyCommand.Path 
[Void]( ni "$path\Solutions" -type Directory -force ) 

$farm.Solutions |% { 
  Write-Host $_.Name 
  $solution = $_.SolutionFile 
  $solution.SaveAs("$path\solutions\$($solution.Name)") 
} 

No comments:

Related Posts with Thumbnails