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

Importing and applying a design package

With SharePoint 2013, a user only needs to be a site collection administrator to apply a packaged design rather than be a farm administrator. This offloads the burden of applying site collection level designs from farm administrators and makes it simpler for site collection administrators to obtain packaged designs from third parties and apply them.

How to do it...

Follow these steps to import and apply a design package:

  1. Navigate to the site in your preferred web browser.
  2. Select Site settings from the Settings menu.
  3. Select Import Design Package from the Look and Feel section.
    How to do it...
  4. Select the design package to import.
  5. Select Import.

How it works...

Importing a design package adds the SharePoint solution file (WSP) to the Solutions Gallery of the site collection and applies the customizations it contains. These SharePoint solutions are sandboxed solutions that allow the site collection administrators to upload and deploy them rather than requiring a farm administrator.

There's more...

A design package may also be imported and applied with PowerShell or with code using the server-side object model.

Importing and applying a design package using PowerShell

Follow these steps to import and apply a design package using PowerShell:

  1. 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")
    
  2. Get the site collection using the Get-SPSite Cmdlet.
    $site = Get-SPSite http://sharepoint/sitecollection
    
  3. Specify the path to the design package WSP file and get the file name 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 version of the design package.
    $package = New-Object Microsoft.SharePoint.Publishing.DesignPackageInfo($fileName, [Guid]::Empty, 1, 0)
    
  5. Create a temporary folder in the RootWeb site to upload the design package to:
    $tempFolderName = "temp_designupload_" + ([Guid]::NewGuid).ToString()
    
    $tempFolder = $site.RootWeb.RootFolder.SubFolders.Add($tempFolderName)
    
  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)
    
    $file = $tempFolder.Files.Add($fileName, $fileBinary, $true)
    
    $fileBinary.Close()
    
  7. 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)
    
  8. Delete the temporary folder.
    $tempFolder.Delete()
    
  9. Use the Dispose method to discard the SPSite object.
    $site.Dispose()
    

Importing and applying a design package with code using the server-side object model

Follow these steps to import and apply a design package with code using the server-side object model:

Note

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

  1. Get the site collection in a using statement.
    using (var site = new SPSite("http://sharepoint/sitecollection"))
  2. Get the root site of the site collection in a using statement.
    using (var web = site.RootWeb)
  3. Specify the path to the design package WSP file and get the file name from the path.
    var filePath = "C:\My Code Design-1.0.wsp";
    
    var fileName = 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.
    var package = new DesignPackageInfo(fileName, Guid.Empty, 1, 0);
  5. Create a temporary folder in the RootWeb site to upload the design package to.
    var tempFolderName = "temp_designupload_" + Guid.NewGuid().ToString();
    
    var tempFolder = web.RootFolder.SubFolders.Add(tempFolderName);
  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.
    var fileBinary = File.OpenRead(filePath);
    
    var file = tempFolder.Files.Add(fileName, fileBinary, true);
    
    var fileBinary.Close();
  7. 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.
    DesignPackage.Install(site, package, file.Url);
  8. Delete the temporary folder.
    tempFolder.Delete();

See also