Wednesday, February 29, 2012

PowerShell script to add Publishing Pages

Following PowerShell script can be used to add pages to the pages Library. The page details are saved in an xml file. And also pagers are checked in and approve using the same script. 

PowerShell script:

void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function New-SPPage{
[CmdletBinding()]
                Param(
                [Parameter(Mandatory=$true)]
                [string]$SiteUrl,   
                [Parameter(Mandatory=$false)]
                [switch]$CreateFromXml,
                [Parameter(Mandatory=$false)]
                [string]$XmlInput
    )
    # Create From Xml
    if ($CreateFromXml) {
        # Read in list of pages from XML
        [xml]$pagesXML = Get-Content $($XmlInput)
        if ($pagesXML -eq $null) { return } 

        # Get publishing web
        $siteUrl = $pagesXML.configuration.siteUrl
        $regionUrl =$pagesXML.configuration.regionUrl
        $site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
        $psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
        # Loop through each web node to extract filename
        $pagesXML.configuration.webs.web | ForEach-Object {
            $webUrl = [string]$_.GetAttribute("url")
            $fullUrl = $siteUrl + $regionUrl +$webUrl
            $sitecol = New-Object Microsoft.SharePoint.SPSite($fullUrl)
            $web = $sitecol.OpenWeb()
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pagesnode = $_.pages

            #loop through each page node to get the page properties
            $pagesnode.page | ForEach-Object {
                $PageTitle = [string]$_.pageTitle
                $PageUrl = [string]$_.pageUrl
                $PagePublishingPageImage =[string]$_.PublishingPageImage  
                $PublishingPageContent = [string]$_.PublishingPageContent
                $PublishingPageContent2 = [string]$_.PublishingPageContent2
                $PageLayoutCT = [string]$_.pageLayoutCT
                $PageLayoutName =[string]$_.pageLayoutName  
                $PageHeading = [string]$_.pageHeading
                $ctype = $psite.ContentTypes[$PageLayoutCT]
                $layout = $psite.GetPageLayouts($ctype, $true) | where{$_.name -eq  $PageLayoutName}              

                Write-Host "Creating $($PageTitle)"
                # Create blank page
                $pages = $pWeb.GetPublishingPages($pWeb)
                            $page = $pages.Add($PageUrl, $Layout)
                            #$newPage = $pWeb.AddPublishingPage($PageUrl,$PageLayout)
                $page.Update()
                # Update the filename to the one specified in the XML
                $item = $page.ListItem
                 $item["Title"] = $PageTitle;
                 $item["Page Image"] = $PagePublishingPageImage;
                 $item["Page Content"] = $PublishingPageContent;
                 $item["Page Content 2"] = $PublishingPageContent2
                 $item["Page Heading"] = $PageHeading;
                 $item.Update()
                # Check-in and publish pages
                CheckInPage -page $page -web $web
                # and approve pages
                ApprovePage -page $page
            }#End ForEach Pages
        $web.Dispose()
        $sitecol.Dispose()
        } #End ForEach Loop Web
        # Dispose of the web
        $site.Dispose()
    } #End CreateFromXml
} #End function 

function CheckInPage ($page, $web)
{
    #Check to ensure the page is checked out by you, and if so, check it in
    if ($page.ListItem.File.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)
    {
        $page.CheckIn("Page checked in automatically by PowerShell script")
        write-host $page.Title"("$page.Name") has been checked in"
    }
    else
    {
        write-host $page.Title"("$page.Name") has not been checked in as it is currently checked out to"$page.ListItem.File.CheckedOutBy
    }
}

function ApprovePage ($page)
{
    if($page.ListItem.ListItems.List.EnableModeration)
    {
        #Check to ensure page requires approval, and if so, approve it
        if ($page.ListItem["Approval Status"] -eq 0)
        {
             write-host $page.Title"("$page.Name") is already approved"
        }
        else
        {
            $page.ListItem.File.Approve("Page approved automatically by PowerShell script")
            write-host $page.Title"("$page.Name") has been approved"
        }
    }
    else
    {
        write-host $page.Title"("$page.Name") does not require approval in this site"
    }
} 

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -EA "SilentlyContinue" 
new-sppage -createfromxml -xmlinput "C:\Projects\BuildScripts\ArticlePages.xml" -siteurl <siteUrl>
Write-Host "Press any key to continue ..." 
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Add your <siteUrl> and the  correct path to the xml file
xml file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <siteUrl>http://<siteurl></siteUrl> 
  <webs>
    <web url="/Test">
      <pages>     
        <page>
          <pageTitle>Article 1</pageTitle>
          <pageUrl>pages/Article 1.aspx</pageUrl>
          <pageHeading>Our Employee Referral Program</pageHeading>
          <PublishingPageImage>&lt;img alt=&quot;HR News&quot; src=&quot;/_layouts/Images/ SampleImages/Sample.png&quot; style=&quot;BORDER: 0px solid; &quot;&gt;</PublishingPageImage>
          <PublishingPageContent>
                Mauris sem eros, pellentesque eu gravida a, congue quis ipsum. In at odio iaculis odio
          </PublishingPageContent>        
          <PublishingPageContent2>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in felis elit. Etiam a ante a
          </PublishingPageContent2>
          <pageLayoutCT>News Article</pageLayoutCT>
          <pageLayoutName>TestPageLayout.aspx</pageLayoutName>
        </page>
        <page>              
          <pageTitle>Article 2</pageTitle>
          <pageUrl>pages/Article 2.aspx</pageUrl>
          <pageHeading>Article 2</pageHeading>
         <PublishingPageImage>
&lt;img alt=&quot;HR News&quot; src=&quot;/_layouts/Images /SampleImages/Sample.png&quot; style=&quot;BORDER: 0px solid; &quot;&gt;
         </PublishingPageImage>
          <PublishingPageContent>
                Nunc posuere lorem a turpis pellentesque pulvinar. In nec pretium neque.
          </PublishingPageContent>
         <PublishingPageContent2>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet risus urna
          </PublishingPageContent2>
          <pageLayoutCT>News Article</pageLayoutCT>
          <pageLayoutName>TestPageLayout.aspx</pageLayoutName>
        </page>  
      </pages>
    </web>
  </webs>
</configuration>                 

No comments:

Post a Comment