Featured image of post Reconnect multi lingual page after copying with ShareGate

Reconnect multi lingual page after copying with ShareGate

Fix issue of disconnected language variations when copying pages with ShareGate using PowerShell. Learn how in this blog post.

Step-by-Step Explanation of the Code

Here’s a step-by-step explanation of the PowerShell code for reconnecting pages with language variations after copying them with ShareGate:

  1. First, we need to connect to the SharePoint site where the pages are located. We can do this using the Connect-PnPOnline cmdlet:
1
Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/MySite"
  1. Next, we need to get a list of all the pages in the site. We can do this using the Get-PnPFolderItem cmdlet with the right language folder:
1
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl SitePages/de 

This code gets a list of all the pages in the “Site Pages” language folder.

  1. Now that we have a list of all the pages, we need to loop through each page and reconnect its language variations. We can do this using a foreach loop:
1
2
3
foreach ($Page in $Pages) {
    # Reconnect language variations for the page
}
  1. Inside the foreach loop, we need to get the default file for the page. We can do this using the Get-PnPFile cmdlet:
1
$DefaultFile = Get-PnPFile -Url "SitePages/$($Page.Name)" -AsListItem

This code gets the default Page by using the Name property from the $Page object.

  1. Next, we need to get additional informations from the language variations of the page. We can do this using the Get-PnPListItem cmdlet:
1
$LangFile = Get-PnPFile -Url SitePages/$Language/$($Page.Name) -AsListItem

This code gets a information about language variations page by filtering the pages in the “Site Pages” library by Folder $Language-Variable and the $Page.Name.

  1. Now that we have all the information we need, we can reconnect the language variations to the default file. We can do this using the Set-PnPListItem cmdlet:
1
2
    #Set the Default Item field
    Set-PnPListItem -List SitePages -Identity $DefaultFile.Id -Values @{"_SPTranslatedLanguages" = $LangISO}
1
2
    #Set the Language Item fields
    Set-PnPListItem -List SitePages -Identity $LangFile.Id -Values @{'_SPIsTranslation' = '1'; '_SPTranslationLanguage' = $LangISO; '_SPTranslationSourceItemId' = $DefaultFile.FieldValues.UniqueId }

This code sets the _SPTranslationSourceItemId property of the variation file to the GUID of the default file.

  1. That’s it! We’ve now looped through all the pages and their language variations, and reconnected them to each other.

Conclusion

In this blog post, we’ve walked through the PowerShell code for reconnecting pages with language variations after copying them with ShareGate. By using this code, you can ensure that the language variations are properly connected to each other, and that they are displayed correctly on the site.

Here’s the complete PowerShell script for reconnecting pages with language variations after copying them with ShareGate:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$SiteCollection = "https://contoso.sharepoint.com/sites/SiteCollection"
$Language = "de"
$LangISO = "de-de"

#Create Connection
Connect-PnPOnline -Url $SiteCollection -Interactive

#Get all aspx pages within Folder DE in Library SitePages
$Pages = Get-PnPFolderItem -FolderSiteRelativeUrl SitePages/$Language

#Loop through all files and get the file object from the files with the same name in the root directory
foreach ($Page in $Pages) {
    $DefaultFile = Get-PnPFile -Url SitePages/$($Page.Name) -AsListItem
    $LangFile = Get-PnPFile -Url SitePages/$Language/$($Page.Name) -AsListItem
    #Set the Default Item field
    Set-PnPListItem -List SitePages -Identity $DefaultFile.Id -Values @{"_SPTranslatedLanguages" = $LangISO}
    #Set the German Language Item fields
    Set-PnPListItem -List SitePages -Identity $LangFile.Id -Values @{'_SPIsTranslation' = '1'; '_SPTranslationLanguage' = $LangISO; '_SPTranslationSourceItemId' = $DefaultFile.FieldValues.UniqueId }
}

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.