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: http://ikarstein.wordpress.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 (http://ikarstein.wordpress.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 Response to “Create WSP (Cab) file on SharePoint Server using SharePoint Built-in functionality”

  1. Ajeet Kumar Singh

    Excellent article. It helped me.
    I have posted your above solution on my blog for my use. I have also referenced your link address. Please let me know if any issue.

    Reply

Leave a Reply