How to Extract WSPs From SharePoint Central Admin Using C#

Introduction

This article explains how to extract the WPSs from a SharePoint Central Admin using server-side C# coding.

Prerequisites

Ensure you have SharePoint Server.

Use the following procedure:

  1. Create a console application.
  2. Open Visual Studio as an Administrator.
  3. Click on "File" -> "New" -> "Project...".

    Project

    console

  4. Select Console Application and enter the name as “ExtractWSP” and click on Ok.

  5. Go to Solution Explorer and right-click on References and add the following references.

    add reference

  6. Microsoft.SharePoint.dll (this will be available in 14 hive folder/ISAPI).

    SharePoint dll

  7. Add the following using references.
    1. using Microsoft.SharePoint;  
    2. using Microsoft.SharePoint.Administration; 
  8. Now copy and paste the code in the Main program.
    1. static void Main(string[] args)  
    2. {  
    3.   
    4.     Console.WriteLine("Processing");  
    5.     try  
    6.     {  
    7.         SPSolutionCollection allSolutions = SPFarm.Local.Solutions;  
    8.         foreach (SPSolution mySolution in allSolutions)  
    9.         {  
    10.             SPPersistedFile myWSP = mySolution.SolutionFile;  
    11.   
    12.             myWSP.SaveAs("C:\\SiteAnalysis\\" + mySolution.Name);  
    13.   
    14.         }  
    15.         Console.WriteLine("Completed");  
    16.     }  
    17.     catch (Exception ex)  
    18.     {  
    19.   
    20.         Console.WriteLine("Error: " + ex.Message + ex.StackTrace);  
    21.         Console.ReadLine();  
    22.     }  
    23.   
    24.         Console.ReadLine();  
    25.   
    26.   
    27. }

  9. Create a folder called “SiteAnalysis” in the C drive.

  10. To make the solution work in the SharePoint 2010 environment, the following properties need to be set.

      Under Application tab Target FrameWork should be “.Net FrameWork 3.5”.

      Under Build tab Platform Target should be set to “Any CPU”.

    Build

Testing

  1. Now to run the application click on the "Play" button.

    Play

  2. The WPS’s that are available in the Central Admin will be saved under the following folder “C:\SiteAnalysis\”.

Summary

Thus in this article you saw how to extract the WSPs from the central admin using server-side code.