Jaa


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 - https://msdn.microsoft.com/en-us/library/jj822363.aspx.

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 - https://www.microsoft.com/en-us/download/details.aspx?id=35585
$Username = "admin@tenant.onmicrosoft.com"
$Site = "https://tenant.sharepoint.com/sites/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
$Context.Load($SC)
$Context.ExecuteQuery()

$RootWeb = $SC.RootWeb
$Context.Load($RootWeb)
$Context.ExecuteQuery()

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

#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);
$FileInfo.Stream.CopyTo($WriteStream)
$WriteStream.Close();

Brendan Griffin - @brendankarl

Comments

  • Anonymous
    January 01, 2003
    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.
  • Anonymous
    December 05, 2015
    The comment has been removed
  • Anonymous
    December 07, 2015
    Sure, here you go - https://github.com/brendankarl/Office-365-PowerShell/blob/master/Design%20Manager/Apply-DesignPackage.ps1
  • Anonymous
    December 08, 2015
    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?
  • Anonymous
    December 09, 2015
    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!
  • Anonymous
    December 11, 2015
    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.
  • Anonymous
    December 16, 2015
    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.