Problem Statement : As we are aware we don't have any Schedule Publishing features for Modern SharePoint Pages. Still user wanted to have scheduled Publishing of News Article.
Solution: We have created a custom Column Property name called PublishDate for Site Pages Library as below screen shot.

we have asked News Publishers to follow the below steps for Schedule Publishing if they want to do.
Step 1 : Create a Site page
Step 2 : Edit the content as they wanted
Step 3 : save the article as draft
Step 4 : edit the site page properties and update the Column "PublishDate" with their desired date and Time of the Publishing as shown in above pic.
Task Scheduler : we run a scheduler at every 15 min to check whether any Saved Draft article or News is available for Publishing. scheduler run a Powershell script as below.
# A script to check for all draft site pages where Published date is non empty
# we will publish those pages and promote as new article
# we will publish those pages and promote as new article
# Created 26-June-2018 vinit kumar
# Check that we are connected to sharepoint Online
$UserName="vinit@test.onmicrosoft.com"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$URL="https://vinit.sharepoint.com/sites/NewsCenter"
$SiteListName="Site Pages"
$Logpath="C:\\Log.txt";
$currentDate=(Get-Date).ToString();
$currentDate=(Get-Date).ToString();
$SPCredentials = New-Object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$Password
$currentDate +"-:Connect Start" | out-File $Logpath -Append
Connect-PnPOnline -Url $URL -Credentials $SPCredentials
$currentDate +"-:Connect Ends" | out-File $Logpath -Append
$getdraftItems = Get-PnPListItem -List $SiteListName -Query "
<View>
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='_ModerationStatus'/>
<Value Type='ModStat'>Draft</Value>
</Eq>
<IsNotNull>
<FieldRef Name='PublishDate'/>
</IsNotNull>
</And>
</Where>
</Query>
</View>"
$currentDate +"-:Query Call Success" | out-File $Logpath -Append
ForEach ($item in $getdraftItems) {
$curretDateandTime=Get-Date
if( $item.FieldValues.PublishDate -le $curretDateandTime){
$FileName=$item.FieldValues.FileLeafRef.Replace('.aspx','')
try{
Set-PnPClientSidePage -Identity $FileName -Publish -PromoteAs NewsArticle
$currentDate +"-:Success Promoting the Page "+$FileName | out-File $Logpath -Append
}
catch{
$currentDate +"-:Error Promoting the Page "+$FileName | out-File $Logpath -Append
}
}
}
try{
Set-PnPClientSidePage -Identity $FileName -Publish -PromoteAs NewsArticle
$currentDate +"-:Success Promoting the Page "+$FileName | out-File $Logpath -Append
}
catch{
$currentDate +"-:Error Promoting the Page "+$FileName | out-File $Logpath -Append
}
}
}
Exit
Script check if article Moderation Status is "Draft" and Current Date and Time is greater than the Published date then using Modern Client side page script first we publish the Article and then Promote as News.
This is a workaround since scehdule Publishing is not available currently but might available in future.
If you like you can use this solution or share.
Happy Modern site
No comments:
Post a Comment