Last active
October 24, 2019 15:03
-
-
Save jcallaghan/ce1d30f5be84ec1060ea96b889767434 to your computer and use it in GitHub Desktop.
This function changes the name of the current homepage and renames a page to Home.aspx. It does of course assume the current home page is Home.aspx. The welcome page URL is case sensitive. See https://jcallaghan.com/2019/06/switch-between-modern-sharepoint-homepages-using-pnp-powershell/ or more information. #Website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Easily replace the current homepage with an alternative page you have created. | |
.DESCRIPTION | |
This function changes the name of the current homepage and renames a page to Home.aspx. | |
The script currently only works when the homepage is /sitepages/home.aspx. | |
It does of course assume the current homepage is Home.aspx. The welcome page URL is case sensitive. | |
A switch to remove the previous homepage rather than rename it is also included. | |
.EXAMPLE | |
Set-NewHomepage -siteurl https://demo.sharepoint.com/sites/intranet -pagerelativeurl /sitepages/newhome1.aspx -removeprevious:$false | |
.NOTES | |
Author: James Callaghan (@jamescallaghan) | |
Website: www.jcallaghan.com | |
Date: 08/06/2019 | |
#> | |
function Set-SPNewHomepage { | |
[cmdletbinding()] | |
Param ( | |
[Parameter(Position=0,Mandatory=$true, | |
HelpMessage = "Provide the url to the SharePoint site where the pages are located.")] | |
[string]$siteurl, | |
[Parameter(Position=1,Mandatory=$true, | |
HelpMessage = "Provide the relative url to the page that should become the homagepage i.e. /sites/abc/xyz.aspx")] | |
[string]$pagerelativeurl, | |
[Parameter(Mandatory=$false, | |
HelpMessage = "Confirm if you wish to remove the previous homepage.")] | |
[bool]$removeprevious | |
) | |
Process { | |
Clear-Host | |
# Pnp Connect | |
write-host "Connecting to site $($siteurl)..." | |
Connect-PnPOnline -Url $siteurl -UseWebLogin | |
# Get web to get the relative url | |
$web = Get-PnpWeb | |
$currenthome = Get-PnPHomePage | |
Write-Host "The current homepage is $($currenthome)." | |
write-host "Renaming $($pagerelativeurl) to make it the new homepage..." | |
# Stop if the homepage is not as the expected home.aspx. | |
# This could be updated to chage the welcome page property. | |
if($currenthome -ne "SitePages/Home.aspx"){ | |
write-host "Error homepage not not as expected." -ForegroundColor Red | |
break | |
} | |
# Rename the page if remove previous is false | |
if($removeprevious -eq $false){ | |
# Create a unique text string to avoid duplicate page names | |
$now = Get-Date | |
$nowstring = $now.ToString("yyyyMMddHHmmss") | |
# Check proposed home page exists | |
$baseurl = $($web.url).Split("/") | |
$pageExists = Get-PnPFile -url $("https://" + $baseurl[2] + $pagerelativeurl) -ErrorAction SilentlyContinue | |
if($null -eq $pageExists){ | |
write-host "Error page not found." -ForegroundColor Red | |
break | |
} | |
# Rename existing home page | |
Rename-PnPFile -ServerRelativeUrl $($web.ServerRelativeUrl + "/sitepages/Home.aspx") -TargetFileName $("oldhome" + $nowstring + ".aspx") -Force | |
} | |
else | |
{ | |
# Remove the previous homepage but recycle it | |
Remove-PnpFile -ServerRelativeUrl $($web.ServerRelativeUrl + "/sitepages/Home.aspx") -Recycle -Force | |
} | |
# Rename page being promoted as the home page | |
Rename-PnPFile -ServerRelativeUrl $pagerelativeurl -TargetFileName "Home.aspx" -Force | |
write-host "New homepage set successfully." -ForegroundColor Green | |
# Disconnect | |
Disconnect-PnPOnline | |
} | |
} | |
.\Set-SPNewHomepage.ps1 -siteurl "https://demo.sharepoint.com/sites/contosoweb/" -pagerelativeurl "/sites/contosoweb/SitePages/NewHome1.aspx" -removeprevious:$false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment