SharePoint Online: Page/List operation with PnP Powershell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Example: Retrieving page

$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
if($page -ne $null) {
	Write-Host "Page exists" -ForegroundColor Green
}
else {
	Write-Host "Page does not exist..." -ForegroundColor Red
}

Example: Retrieving all pages

$pages = Get-SPOListItem -List "Pages"  
foreach($page in $pages){  
    #Page Properties
    $page["Title"]  
    $page["FileRef"]
    #etc...
}  

Example: Adding a page

Add-SPOPublishingPage -PageName "testPage" ` 
                      -PageTemplateName "BlankWebPartPage" `
                      -Title "testPage" -Publish  

Example: How to update page properties

This can be also used to update the page layout of a publishing page


#Values type and reference from PnP site
#Single line of text: -Values @{"Title" = "Title New"}
#Multiple lines of text: -Values @{"MultiText" = "New text\n\nMore text"}
#Rich text: -Values @{"MultiText" = "New text"}
#Choice: -Values @{"Choice" = "Value 1"}
#Number: -Values @{"Number" = "10"}
#Currency: -Values @{"Number" = "10"}
#Currency: -Values @{"Currency" = "10"}
#Date and Time: -Values @{"DateAndTime" = "03/10/2015 14:16"}
#Lookup (id of lookup value): -Values @{"Lookup" = "2"}
#Yes/No: -Values @{"YesNo" = "No"}
#Person/Group (id of user/group in Site User Info List or email of the user, seperate multiple values with a comma): -Values @{"Person" = "user1@domain.com","21"}
#Hyperlink or Picture: -Values @{"Hyperlink" = "https://github.com/OfficeDev/, OfficePnp"}

#retrive the page
$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
#Update the page
Set-SPOListItem -List "Pages" -Web $web -Identity $page `
                -Values @{"Title"="Test Title"}

#Or alternatively if you know the page ID, e.g: 1
Set-SPOListItem -List "Pages" -Web $web ` 
                -Identity 1 -Values @{"Title"="Test Title"}

Example: Updating Publishing Page Layout

#retrieve page
$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web

#check out the file for editing
Set-SPOFileCheckedOut -Web $Web -Url $page["FileRef"]

#updating properties
Set-SPOListItem -List "Pages" -Web $Web -Identity $page -Values @{"https://yourspsite.sharepoint.com/_catalogs/masterpage/BlankWebpartPage.aspx, Blank Web Part Page"}

#check in the update
Set-SPOFileCheckedIn -Web $Web -Url $page["FileRef"] -CheckinType MajorCheckIn -Comment "Checked in by script"   

Example: Deleting Page

$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
Remove-SPOListItem -List Pages -Identity $page -Web $web
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s