Featured image of post Re-creating Multilanguage Links: A Step-by-Step Guide

Re-creating Multilanguage Links: A Step-by-Step Guide

A How-to guide to re-creating multilanguage links for Pages in your SitePages library.

Maybe you’ve also stumbled upon the issue of missing multilanguage links in your SitePages library in SharePoint Online. This can happen if you create translations in an unsaved default language Post / News Article.
Since the Column Translation languages (_SPTranslatedLanguages) is read-only, you can’t simply add the missing language tags to the existing pages. Instead, you need for example PowerShell to re-create the entries for each page. This is where my new PowerShell script comes into play. It helps you to re-create all missing multilanguage links in your SitePages library by checking if the existing translations are correctly linked to the default language page. If not, it updates the _SPTranslatedLanguages field accordingly.

Prerequisites

  • You need to have the PnP.PowerShell module installed.
  • You need the necessary permissions to run the script.

Step-by-Step Documentation

1. Set Up Variables

  • TenantName: Use your tenant name.
  • SiteName: Define the name of the site collection to be flagged as a Corporate News Site.
1
2
$TenantName = 'yourtenantname'
$SiteName = 'YourSiteName'

2. Connect to SharePoint Online

  • Use PnP-PowerShell to connect to the SharePoint Online site.
1
Connect-PnPOnline -Url "https://$($TenantName).sharepoint.com/sites/$SiteName" -interactive

3. Retrieve Language Folders

  • Retrieve all folders from the SitePages library where the folder name contains exactly two letters. This “trick” I use to identify the language folders. So if you have also other folders in your SitePages library, with only two letters, you should adjust the script.
1
$LanguageFolders = Get-PnPFolderInFolder -Identity 'SitePages' | Where-Object { $_.Name -match '^[a-z]{2}$' }

4. Process Each Language Folder

  • the script looks in each language folder, to retrieve all pages.
1
2
foreach ($LanguageFolder in $LanguageFolders) {
    $Pages = Get-PnPListItem -List 'SitePages' -FolderServerRelativeUrl "/sites/$SiteName/SitePages/$($LanguageFolder.Name)"

5. Retrieve and Update Default Pages

  • For each translated page, we now have to retrieve the corresponding default language page (in the root directory).
  • Then we need to check if the column _SPTranslatedLanguages already contains this language.
  • If not, we add it to the _SPTranslatedLanguages field. We use the SystemUpdate method to update the field without changing the modified date. If you want / have to to update the modified date, you can remove the -UpdateType.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    foreach ($Page in $Pages) {
        # Retrieve the default page in the root directory
        $DefaultPage = Get-PnPListItem -List 'SitePages' -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$($Page.FieldValues.FileLeafRef)</Value></Eq></Where></Query></View>"

        # Check if the default page exists and if the translation language is already linked
        if ($DefaultPage) {
            if ($DefaultPage.FieldValues._SPTranslatedLanguages -cnotcontains $Page.FieldValues._SPTranslationLanguage) {
                $OldLanguages = $DefaultPage.FieldValues._SPTranslatedLanguages
                $NewLanguages = $OldLanguages + $Page.FieldValues._SPTranslationLanguage
                Set-PnPListItem -List 'SitePages' -Identity $DefaultPage.Id -Values @{'_SPTranslatedLanguages' = $NewLanguages } -UpdateType SystemUpdate
            }
        }
    }
}

Full Script

 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
$TenantName = 'yourtenantname'
$SiteName = 'YourSiteName'

Connect-PnPOnline -Url "https://$($TenantName).sharepoint.com/sites/$SiteName" -interactive

$LanguageFolders = Get-PnPFolderInFolder -Identity 'SitePages' | Where-Object { $_.Name -match '^[a-z]{2}$' }

foreach ($LanguageFolder in $LanguageFolders) {
    $Pages = Get-PnPListItem -List 'SitePages' -FolderServerRelativeUrl "/sites/$SiteName/SitePages/$($LanguageFolder.Name)"

    foreach ($Page in $Pages) {
        # Retrieve the default page in the root directory
        $DefaultPage = Get-PnPListItem -List 'SitePages' -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$($Page.FieldValues.FileLeafRef)</Value></Eq></Where></Query></View>"

        # Check if the default page exists and if the translation language is already linked
        if ($DefaultPage) {
            if ($DefaultPage.FieldValues._SPTranslatedLanguages -cnotcontains $Page.FieldValues._SPTranslationLanguage) {
                $OldLanguages = $DefaultPage.FieldValues._SPTranslatedLanguages
                $NewLanguages = $OldLanguages + $Page.FieldValues._SPTranslationLanguage
                Set-PnPListItem -List 'SitePages' -Identity $DefaultPage.Id -Values @{'_SPTranslatedLanguages' = $NewLanguages } -UpdateType SystemUpdate
            }
        }
    }
}

Disconnect-PnPOnline

I hope this script helps you to re-create the missing multilanguage links in your SitePages library. If you have any questions or feedback, feel free to leave a comment below.

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.