Thursday 12 December 2013

Create Publishing web, Pages, approve them

My Task is to copy a bunch of publishing pages into an "Archived Site", before you think the obvious that is just a dumb approach, this script is for a bull headed client. Rather then try and explain, it's just best to give them what they want even if it's not what they need. To get started I created a script that would populate a newsroom subsite with subsites for each month and add 5 checked in, approved pages to each of the subsites. Here is that Script:



$months = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"

#delete the subsites
foreach($month in $months)
{
       $w = get-SPWeb http://ssrap1:8080/NewsRoom/$month
       Remove-SPWeb -Identity $w -Confirm
}

#create the subsite for the newsroom
foreach($month in $months)
{
       $SiteTemplate = get-SPWebTemplate | where {$_.Title -eq "Publishing Site with Workflow"}
      
       $web = New-SPWeb -Url http://ssrap1:8080/NewsRoom/$month  -Name $month -Template $SiteTemplate
       Write-Host "`nCreate SubSite: $web"
      
       $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
      
       $i = 0
       #create 5 pages for subsite
       Write-Host Createing Pages for $month subsite
       while ($i -le 5)
       {
              $newPage = $pWeb.AddPublishingPage();
              $newPage.CheckIn("Checked in")
              $i++
       }

       $pages = $pWeb.GetPublishingPages()
      
       #approve all pages
       foreach($p in $pages)
       {
                $p.ListItem.File.Approve("Page approved automatically by PowerShell script")
       }            

       $web.dispose()
}