Friday 13 December 2013

Move Publishing Pages

In my last post I laid out the requirements of my script and a script to set up a source site to copy from, now here is the script that does the movement of all documents, images, and pages from a web that has a bunch of subwebs where the news articles are stored. Again I know this is not the SharePoint way, but it's the way the thing is set up, my job was not to fix this.

<#
.Synopsis
   Script to archive newsroom assets
.DESCRIPTION
   Script to migrate Newsroom Assets to archive Folder
.EXAMPLE
   Copy-spmNewsroomAssets -SourceWebUrl http://spm/newsroom -DestinationWebUrl http://spm/archive/2013
.EXAMPLE
   Copy-spmNewsroomAssets -SourceWebUrl http://spm/newsroom -DestinationWebUrl http://spm/archive/2013 | Out-File c:\Failed.txt
#>

function Copy-SpmNewsroomAssets
{
    [CmdletBinding()]
    Param
    (
        # Specify the newsroom web, it should poses subwebs for each month ie http://www.spm/newsroom
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false, Position=0)]
              [string]
        $SourceWebUrl,

        # Specify the archive web ie http://www.spm/Archive/2013
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false, Position=1)]
              [string]
        $DestinationWebUrl
    )

    Begin
    {
              $SourceWeb = Get-SPWeb $SourceWebUrl
              $DestinationWeb = Get-SPWeb $DestinationWebUrl
              $folders = "Documents", "PublishingImages", "Pages"
    }
    Process
    {
              $folders | % {
                     Move-Files -SourceWeb $SourceWeb -DestinationWeb $DestinationWeb -folder $_
              }
    }
    End
    {
              $SourceWeb.Dispose()
              $DestinationWeb.Dispose()
    }
}

function Move-Files($SourceWeb, $DestinationWeb, $folder)
{
      
       $DestinationFolder = $DestinationWeb.GetFolder($folder)
      
       $SourceWeb.webs | % {
              $SourceLibrary = $_.GetFolder($folder)
                      
              foreach($file in $SourceLibrary.Files)
              {
                     if(!(Check-FileExists -filename $file.Name -folder $DestinationFolder))
                     {
                            $destinationFile = $DestinationFolder.Files.Add($DestinationFolder.Url + "/" +$file.Name, $file.OpenBinary(), $file.Properties, $true)    
                           $Properties = "Author", "Created", "Editor", "Modified", "PublishingPageLayout"
                          
                           Copy-MetaData -sourceItem $file.Item -DestinationItem $destinationFile.Item -Properties $Properties
                     }
                     else
                     {
                           $SourceFile = $SourceWeb.Site.Url + $SourceLibrary.ServerRelativeUrl + "/" + $file.Name
                           $Destination = $DestinationWeb.Site.Url + $DestinationFolder.ServerRelativeUrl
                           Write-Output "$SourceFile already exists in $Destination"
                     }
              }
       }
}

function Copy-MetaData($sourceItem, $DestinationItem, $Properties)
{
       foreach($property in $Properties)
       {
              if($sourceItem.Fields.ContainsField($property))
              {
                     $DestinationItem[$property] = $sourceItem[$property]
              }     
       }
      
       $DestinationItem.UpdateOverwriteVersion()
       $DestinationItem.Systemupdate()  
}

function Check-FileExists($filename, $folder)
{
       $folder.Files | %{
              if($filename -eq $_.Name)
              {
                     return 1
              }     
       }
       return 0
}