We recently had a project that needed 164 SharePoint sites to be created across development, testing, and production environments. The sites would be created prior (with other scripts on the blog here), but other assets needed to be uploaded or synced as well. In order to accomplish the task, I wrote this PowerShell function to copy the page content of one SharePoint page to another.
Example:
1 2 3 |
Copy-SPPageContent -Source_URL https://sharepoint.alphashell.net/sites/subsite1 -Source_Library Pages -Source_Page PageWithContent.aspx -Destination_URL https://sharepoint.alphashell.net/sites/subsite2 -Destination_Library Pages -Destination_Page NewPage.aspx |
The function will check both targets to confirm there is content to copy, and a destination for it. If anything is missing, it will abort and alert you:
If successful, you will see output like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# Function to copy a sharepoint page's content and overwrite an existing page with it. function Copy-SPPageContent{ # Page names must include ".aspx" # Specify parameters param( [Parameter(Mandatory=$true)][string]$Source_URL, [Parameter(Mandatory=$true)][string]$Source_Library, [Parameter(Mandatory=$true)][string]$Source_Page, [Parameter(Mandatory=$true)][string]$Destination_URL, [Parameter(Mandatory=$true)][string]$Destination_Library, [Parameter(Mandatory=$true)][string]$Destination_Page ) # Get source site try{ $source_site = Get-SPWeb $Source_URL -ErrorAction SilentlyContinue }Catch{ write-host -ForegroundColor Black -BackgroundColor White "Couldn't load source site" $source_site = $null return $false } IF(!$source_site){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't load source site" return $false } # Get source library try{ $source_list = $source_site.lists[$Source_Library] }Catch{ write-host -ForegroundColor Black -BackgroundColor White "Couldn't find source library" $source_site.dispose() $source_site = $null $Source_Library = $null return $false } IF(!$source_list){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't find source library" $source_site.dispose() $source_site = $null $Source_Library = $null return $false } # Get source page and content try{ $source_file = $source_list.Items.File | where {$_.name -eq $Source_Page} $source = [string]$source_file.Properties.PublishingPageContent }Catch{} # Double check that the source file exist IF(!$source_file){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't find source page" $source_site.dispose() $source_site = $null $Source_Library = $null $source_file = $null return $false } #### End retrieving source content #### # Get destination site try{ $destination_site = Get-SPweb $Destination_URL -ErrorAction SilentlyContinue }Catch{ write-host -ForegroundColor Black -BackgroundColor White "Couldn't load destination site" $destination_site = $null return $false } IF(!$destination_site){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't load destination site" return $false } # Get destination library try{ $destination_list = $destination_site.lists[$Destination_Library] }Catch{} # Throw error if couldn't load. IF(!$destination_list){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't find destination library" $source_site.dispose() $destination_site.dispose() $destination_site = $null $destination_Library = $null return $false } # Get destination page try{ $destination_file = $destination_list.Items.File | where {$_.name -eq $Destination_Page} $destination = [string]$destination_file.Properties.PublishingPageContent }Catch{ # Throw error if couldn't load. write-host -ForegroundColor Black -BackgroundColor White "Couldn't load destination page" $source_site.dispose() $destination_site.dispose() $destination_site = $null $Destination_Library = $null $Destination_Page = $null return $false } # Double check that the destination file exist IF(!$destination_file){ write-host -ForegroundColor Black -BackgroundColor White "Couldn't find destination page" $source_site.dispose() $destination_site.dispose() $destination_site = $null $destination_Library = $null $destination_file = $null return $false } write-host `n write-host -ForegroundColor Green "Found source and destination targets!" write-host `n write-host -ForegroundColor Yellow "Attempting to checkout file..." Start-Sleep -Milliseconds 200 # Check out the file try{ $destination_file.Checkout() write-host -ForegroundColor Green "Success!" }Catch{ write-host -ForegroundColor Red "Couldn't check out file" return $false } # Set the property and update the page try{ write-host "Updating file..." $destination_file.SetProperty("PublishingPageContent",$source) $destination_file.Update() }Catch{ #If step fails, undo checkout. Return. write-host -ForegroundColor Black -BackgroundColor white "Error.." $destination_file.UndoCheckOut() $source_site.dispose() $destination_site.dispose() write-host -ForegroundColor Black -BackgroundColor White "Checkout was discarded." return $false } # Check in and publish the file try{ Write-Host "Checking file back in..." $Comment = ("Content placed from copied content at " + $Source_URL + "\" + $Source_Library + "\" + $Source_file) $destination_file.CheckIn($Comment) }Catch{} # Publish the file try{ Write-Host "Publishing file..." $destination_file.Publish("Published via powershell") }Catch{} write-host -ForegroundColor Black -BackgroundColor White "Content copy completed." $source_site.dispose() $destination_site.dispose() return $true } |