Book Image

SharePoint 2013 WCM Advanced Cookbook

By : JOHN CHAPMAN
Book Image

SharePoint 2013 WCM Advanced Cookbook

By: JOHN CHAPMAN

Overview of this book

Table of Contents (19 chapters)
SharePoint 2013 WCM Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a web part page and adding web parts with PowerShell


In this recipe, we will use PowerShell to create a new publishing page and add web parts to it. This is useful in instances where a large number of these publishing pages need to be created and doing so one-by-one in the web interface would be a long and tedious process.

How to do it...

Follow these steps to create a web part page and add web parts using PowerShell:

  1. Get the site with the Get-SPWeb Cmdlet.

    $web = Get-SPWeb "http://sharepoint/publishing"
    
  2. Get the publishing site from the SharePoint site.

    $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    
  3. Get the page layout from the publishing site as follows:

    $layout = $pubWeb.GetAvailablePageLayouts() | Where-Object { $_.Title -eq "Blank Web Part Page" }
    
  4. Create the publishing page.

    $page = $pubWeb.AddPublishingPage("PowerShellPageWithWebPart.aspx", $layout)
    
  5. Update the publishing page object using the following command:

    $page.Update()
    
  6. Set the Title property...