Last active
February 20, 2023 15:49
-
-
Save Snaver/7291b9d02eea619a2042c7b41f2eaef6 to your computer and use it in GitHub Desktop.
SharePoint Online News Import PowerShell Script
This file contains hidden or 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
function Create-Slug { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$InputString | |
) | |
# Use a regular expression to remove any characters that are not letters, numbers, or dashes | |
$slug = $InputString -replace '[^\w-]', '' | |
# Convert the string to lowercase | |
$slug = $slug.ToLower() | |
return $slug | |
} | |
# Add-Type -AssemblyName System.Windows.Forms | |
# # Ask the user for the Page input file | |
# $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog | |
# $null = $FileBrowser.ShowDialog() | |
# $page_file_name = $FileBrowser.FileNames | |
# if ( !$page_file_name){ | |
# Write-Host 'No input file was selected, terminating script' | |
# exit # nothing to see here, an input file was not selected | |
# } | |
$SiteURL = "https://....sharepoint.com/sites/..." | |
$rand = Get-Random | |
#Connect to PpP Online | |
Connect-PnPOnline -Url $SiteURL -Interactive | |
# # note: better to us tab delimited files since page names can include commas | |
$Posts = Import-Csv -Path './news-import.csv' | |
foreach($Post in $Posts){ | |
# $name = "News " + $rand | |
# $Date = "2021-12-23" | |
# $text = Get-Content .\test-post.html -Raw | |
# // November 30, 2022, 2:12 pm | |
$date = Get-Date -Date $Post.Date | |
$slug = Create-Slug -InputString $Post.Title | |
# $name = $date.ToString("yyyy-MM-dd") + '-' + $slug | |
$name = $date.ToString("yyyy-MM/") + $Post.id + '-' + $slug | |
$title = $Post.Title | |
# $Date = $Post.Date | |
$text = $Post.Body | |
#Create new page | |
$Page = Add-PnPPage -Name $name -LayoutType Article | |
#Set Page properties | |
Set-PnPPage -Identity $Page -Title $title -CommentsEnabled:$True -HeaderType Default -PromoteAs NewsArticle | |
#Add Section to the Page | |
Add-PnPPageSection -Page $Page -SectionTemplate OneColumn | |
# #Add Text to Page | |
$textPart = Add-PnPPageTextPart -Page $Page -Text $text -Section 1 -Column 1 | |
# Add-PnPPageWebPart -Page $page -Text "Welcome To <strong>News</strong> Portal" -Section 1 -Column 1 -Order 1 | |
#Appear on news | |
#https://learn.microsoft.com/en-us/dotnet/api/officedevpnp.core.pages.promotedstate?view=sharepoint-pnpcoreol-3.2.1810 | |
Set-PnPListItem -List "SitePages" -Id $Page."PageId" -Values @{"PromotedState"="2"} | |
#Set published date | |
#https://babakdanyal.com/edit-original-publishing-date-in-sharepoint-news-posts/ | |
Set-PnPListItem -List "SitePages" -Id $Page."PageId" -Values @{"FirstPublishedDate"=$date.ToString("yyyy-MM-dd HH:mm");} -UpdateType SystemUpdate | |
#Publish the page | |
$Page.Publish() | |
} | |
Get-PnPListItem -List "SitePages" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment