SharePoint 2013 WCM Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Importing a design package to all site collections with PowerShell

Applying a design package to a large number of site collections can be a tedious task. To expedite the process, we can use PowerShell. In this recipe, we are going to use a PowerShell script (PS1) to upload and apply a design package to each content site collection in each web application on the local SharePoint farm.

How to do it...

Follow these steps to import a design package to all site collections with PowerShell:

  1. Open your preferred text editor to create the PS1 script file.
  2. Load the Microsoft.SharePoint.dll and Microsoft.SharePoint.Publishing.dll assemblies into the PowerShell session.
    [Reflection.Assembly]::LoadFrom("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Publishing.dll")
    
    [Reflection.Assembly]::LoadFrom("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.dll")
    
  3. Specify the path to the design package WSP file and get the filename from the path.
    $filePath = "C:\My PowerShell Design-1.0.wsp"
    
    $fileName = [System.IO.Path]::GetFileName($filePath)
    
  4. Create a DesignPackageInfo object to represent the design package we are about to upload. In the constructor, specify the major and minor versions of the design package.
    $package = New-Object Microsoft.SharePoint.Publishing.DesignPackageInfo($fileName, [Guid]::Empty, 1, 0)
    
  5. Create a temporary folder name to upload the design package in each site collection.
    $tempFolderName = "temp_designupload_" + ([Guid]::NewGuid).ToString()
    
  6. Use the OpenRead method of System.IO.File to read the contents of the design package WSP file and add the file to the Files collection of the temporary folder.
    $fileBinary = [System.IO.File]::OpenRead($filePath)
    
  7. Use a foreach loop to iterate through each content SPWebApplication on the local SharePoint farm using the Get-SPWebApplication Cmdlet.
    foreach($webApp in (Get-SPWebApplication))
    
  8. Use a foreach loop to iterate through each SPSite Cmdlet in the Sites property of the SPWebApplication object.
    foreach($site in $webApp.Sites)
    
  9. Verify the CompatibilityLevel property of the SPSite object to ensure it is in SharePoint 2013 (Version 15) mode and not in SharePoint 2010 (Version 14) mode.
    if ($site.CompatibilityLevel –eq 15)
    
  10. Using the following command, create a temporary folder in the RootWeb site to upload the design package:
    $tempFolder = $site.RootWeb.RootFolder.SubFolders.Add($tempFolderName)
    
  11. Add the file to the Files collection of the temporary folder.
    $file = $tempFolder.Files.Add($fileName, $fileBinary, $true)
    
  12. Use the Install method of Microsoft.SharePoint.Publishing.DesignPackage to add the design package to the Solutions Gallery and apply the customizations in the design package to the site collection.
    [Microsoft.SharePoint.Publishing.DesignPackage]::Install($site, $package, $file.Url)
    
  13. Delete the temporary folder.
    $tempFolder.Delete()
    
  14. After the foreach loops are completed, close the design package WSP file.
    $fileBinary.Close()
    
  15. Use the Dispose method to discard the SPSite object.
    $site.Dispose()
    
  16. Save the file as a PS1 file, for example, importdesignpackage.ps1.
  17. Execute the script in the PowerShell session.
    ./importdesignpackage.ps1
    

How it works...

PowerShell provides a scripting environment that can simplify repetitive administrative tasks. Using PowerShell, we are able to use a combination of the Cmdlets provided and the .NET code to iterate through each site collection in each web application to import and apply our design package.

In this recipe, we used the Get-SPWebApplication Cmdlet to retrieve all of the content web applications on the local SharePoint farm. We then iterated through each site collection in the Sites property of each web application. For each site collection, we uploaded the design package to a temporary folder. Lastly, we installed the design package to each site collection from the temporary folder.

There's more...

This recipe may also be accomplished with code using the server-side object model.

Note

A reference to the Microsoft.SharePoint.Publishing.dll assembly is required for this recipe.

Follow these steps to import and apply a design package to all site collections using the server-side object model:

  1. Specify the path to the design package WSP file and get the filename from the path.
    var filePath = "C:\My Code Design-1.0.wsp";
    
    var fileName = Path.GetFileName(filePath);
  2. Create a DesignPackageInfo object to represent the design package we are about to upload. In the constructor, specify the major and minor versions of the design package.
    var package = new DesignPackageInfo(fileName, Guid.Empty, 1, 0);
  3. Create a temporary folder name to upload the design package in each site collection.
    var tempFolderName = "temp_designupload_" + Guid.NewGuid().ToString();
  4. Use the OpenRead method of System.IO.File to read the contents of the design package WSP file and add the file to the Files collection of the temporary folder.
    var fileBinary = File.OpenRead(filePath);
  5. Use a foreach loop to iterate through each content SPWebApplication on the local SharePoint farm.
    foreach(var webApp in SPWebService.ContentService.WebApplications)
  6. Use a foreach loop to iterate through each SPSite in the Sites property of the SPWebApplication object.
    foreach(SPSite site in webApp.Sites)
  7. Verify the CompatibilityLevel property of the SPSite object to ensure it is in SharePoint 2013 (Version 15) mode and not in SharePoint 2010 (Version 14) mode.
    if (site.CompatibilityLevel == 15)
  8. Create a temporary folder in the RootWeb site to upload the design package to.
    var tempFolder = site.RootWeb.RootFolder.SubFolders.Add(tempFolderName);
  9. Add the file to the Files collection of the temporary folder.
    var file = tempFolder.Files.Add(fileName, fileBinary, true);
  10. Use the Install method of DesignPackage to add the design package to the Solutions Gallery and apply the customizations in the design package to the site collection.
    DesignPackage.Install(site, package, file.Url);
  11. Delete the temporary folder.
    tempFolder.Delete();
  12. Discard the SPSite object using the Dispose method.
    site.Dispose();
  13. After the foreach loops are completed, close the design package WSP file.
    fileBinary.Close();

See also