Featured image of post How to use PowerShell to change the publish date of a news page in SharePoint Online

How to use PowerShell to change the publish date of a news page in SharePoint Online

In this blog post, we will explore how to use PowerShell to change the first publish date of a news page in SharePoint Online. This can be useful if you need to update the publish date of a news page to reflect changes in the content or to correct errors in the original publish date.

Prerequisites

Before we get started, there are a few prerequisites that you will need:

  • A SharePoint Online site where you have permission to create and edit news pages.
  • PowerShell 7.2 or later installed on your computer.
  • The PnP.PowerShell module installed on your computer.

The Script

The PowerShell script below demonstrates how to change the first publish date of a news page in SharePoint Online:

 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
# Set the new publish date
$publishDate = '2023-10-12'

# Set the ID of the news item to update
$newsItemId = 361

# Connect to the SharePoint site using PnP-PowerShell
Connect-PnPOnline "https://yourtenant.sharepoint.com/sites/yoursite" -ClientId $AppID -Interactive

# Get the news item in English
$englishNewsItem = Get-PnPListItem -List 'SitePages' -Id $newsItemId

# Update the news item with the new publish date
Set-PnPListItem -List 'SitePages' -Identity $newsItemId -Values @{'FirstPublishedDate' = $publishDate } -UpdateType SystemUpdate

# Check if the news item has any translations
if ($englishNewsItem.FieldValues._SPTranslatedLanguages) {
    # If there are translations, get the UniqueId of the English news item
    $uniqueId = $englishNewsItem.FieldValues.UniqueId
    # Get the translated news items
    $translatedNewsItems = Get-PnPListItem -List 'SitePages' -Query "<View Scope='RecursiveAll'><Query><Where><Contains><FieldRef Name='_SPTranslationSourceItemId'/><Value Type='Guid'>$($uniqueId)</Value></Contains></Where></Query></View>"
    # Update each translated news item
    foreach ($translatedNewsItem in $translatedNewsItems) {
        Set-PnPListItem -List 'SitePages' -Identity $translatedNewsItem.Id -Values @{'FirstPublishedDate' = $publishDate } -UpdateType SystemUpdate
    }
}

The script starts by setting the new publish date and the ID of the news item to update. You will need to modify these values to match your specific scenario.

Next, the script connects to the SharePoint site using PnP-PowerShell and retrieves the news item in English. The script then updates the news item with the new publish date using the Set-PnPListItem cmdlet.

The script then checks if the news item has any translations. If there are translations, the script retrieves the UniqueId of the English news item and uses it to get the translated news items. The script then updates each translated news item with the new publish date using the Set-PnPListItem cmdlet.

Conclusion

In this blog post, we explored how to use PowerShell to change the first publish date of a news page in SharePoint Online. This can be a useful tool for updating the publish date of a news page to reflect changes in the content or to correct errors in the original publish date.

By using PowerShell and the PnP PowerShell module, you can automate this process and save time and effort when managing your SharePoint Online news pages.

Comments

You can use your Mastodon account to reply to this post. Learn how this is implemented here.

Reply to ollimenzel's post

With an account on the Fediverse or Mastodon, you can respond to this post. Since Mastodon is decentralized, you can use your existing account hosted by another Mastodon server or compatible platform if you don't have an account on this one.

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.