Create WSP (Cab) file on SharePoint Server using SharePoint Built-in functionality

SharePoint Server 2010 and Foundation too has a namespace “Microsoft.SharePoint.Utilities.Cab”. In this namespace there are all functions you need to create CAB=WSP files. – I have already used this functionality in a private project: https://blog.kenaro.com/2012/02/29/demo-generating-sandboxed-solutions-through-code-for-sharepoint-2010/

Here is the PowerShell script to create create CAB = WSP files on a SharePoint server:

# Written by Ingo Karstein (https://blog.kenaro.com)
#   v0.1.0.0 / 2012-05-01

$destWSPfilename = "C:UsersAdministratorSourceMyWSP.wsp"
$sourceSolutionDir = [System.IO.DirectoryInfo]"C:UsersAdministratorSourceMyWSP_Content"

##################################

$a = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$ctor = $a.GetType("Microsoft.SharePoint.Utilities.Cab.CabinetInfo").GetConstructors("Instance, NonPublic")[0]

$cabInf = $ctor.Invoke($destWSPfilename );

$mi = $cabInf.GetType().GetMethods("NonPublic, Instance, DeclaredOnly")
$mi2 = $null
foreach( $m in $mi ) {
    if( $m.Name -eq "CompressDirectory" -and $m.GetParameters().Length -eq 4 ) {
        $mi2 = $m;
        break;
    };
}

$mi2.Invoke($cabInf, @( $sourceSolutionDir.FullName, $true, -1,$null ));

One thought on “Create WSP (Cab) file on SharePoint Server using SharePoint Built-in functionality

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.