Office 365 – Automating the Creation of a Design Manager Package using CSOM with PowerShell

One of the really cool features in SharePoint 2013 is the Design Manager, further information on this feature can be found here: Overview of Design Manager in SharePoint 2013 –

Design Manager provides the ability to export a Design Manager Package so that customizations can be easily copied to another Site Collection and re-used – potentially saving a lot of time and hassle, the package itself is a sandbox solution using the WSP file format. A screenshot of the option can be found below:

Wouldn't it be cool if you could automate the creation of a Design Manager Package using PowerShell? Below is a PowerShell script that uses CSOM to perform this very task! The resultant package is saved to a local drive for re-use.

The three highlighted variables need to be updated. $Username is the username of an administrator of the Site Collection, $Site is the URL of the Site Collection and $DestinationDir is the local directory to save the exported package to.

#Please install the SharePoint client components SDK –
$Username = ""
$Site = ""
$DestinationDir = "D:\Downloads\"

#Add references to SharePoint client assemblies and authenticate to Office 365 site
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

$SC = $Context.Site

$RootWeb = $SC.RootWeb

$DP = [Microsoft.SharePoint.Client.Publishing.DesignPackage]::ExportEnterprise($Context,$SC,$False)

#Download Design Package
$Package =  $SC.ServerRelativeUrl + "/_catalogs/Solutions/" + $RootWeb.Title + "-1.0.wsp"
$Destination =  $DestinationDir + $RootWeb.Title + "-1.0.wsp"
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Context,$Package)
$WriteStream = [System.IO.File]::Open($Destination,[System.IO.FileMode]::Create);

Brendan Griffin – @brendankarl

Comments (7)

  1. Anonymous says:

    Build your brand with a website
    You know getting your name out there is key to your business’s success and that a professional-looking website can help do that. But building and maintaining a website that’s an effective marketing tool seems too daunting and expensive for your resources. Meet your Office 365 public website! It’s fast. It’s easy. And it adds no additional hosting fees, so you save on costs.

  2. Chuck B says:

    Very useful, Brendan. Do you have the other half of the code, for loading and applying the package to another site collection? That’s all I’m missing for automating a site creation process that we need. I haven’t wrapped my head around enough CSOM to extrapolate
    from your example yet. Thanks!

  3. Chuck B says:

    Hey thanks. That illuminates a number of points for me. It even made me wonder if I could simply store the .wsp templates in the document library of another site collection. No luck, though. It works if they’re pre-uploaded to a document library in the
    site collection I’m applying it to, but from another library the Install function throws "Value does not fall within the expected range." Do you know if that’s a restriction of the DesignPackage functions?

  4. I’m fairly sure that the WSP needs to reside within the local Site Collection, you could of course store all of the WSPs in a central Site Collection and simply download/upload to the Site to apply – would make the script a little trickier though!

  5. Nayan N says:

    Thanks Brendan. Could you please suggest a script we could use for on-premises SP setup?

    We are facing some problems generating design package. Thanks for helping out.

  6. You should be able to run this against On-Premises if you use the On-Premises Client SDK, you’d need to change the authentication piece though.