How to easily convert to convert data sources to GUID and vice versa through scripts in Sitecore Experience Accelerator?
Sitecore Experience Accelerator has over the time provided the scripts to ease the process for maintaining Sitecore content. Two of them are as follows:
Convert data sources to GUID
Where is it available?
This script is available for items with templates inherits from Page template or Page Data template. Now I run this script.
What does it do?
It converts the data source of your controls to GUID. For example, on my page, I am using Event list control and it has a data source set to "local:/Data/MyEvents".
Now I run the script and it goes through all the controls to convert the data source for all controls to GUID. So you can look at the script results.
So if I look at the final rendering before running the script. It looks like follows:
<r xmlns:p="p" xmlns:s="s" p:p="1"><d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"
><r uid="{789103A1-2331-4EA0-848B-CBAA69CCB213}" s:ds="local:/Data/MyEvents"
s:id="{52E40C93-F091-47C1-84DD-491DBCDC54B5}"
s:par="ListSignature&PageSize&Offset&GridParameters=%7B908E2BC6-C110-4ED7-AF39-7EEACBB31A34%7D&FieldNames&Styles&TitleHeadingLevel=%7B6A44D9C6-61F0-4B69-8E1A-FDC27F96BA0F%7D&Reset Caching Options&RenderingIdentifier&DynamicPlaceholderId=1" s:ph="header" /></d></r>
After I run the script, it looks like follows:
<r xmlns:p="p" xmlns:s="s" p:p="1"><d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"
><r uid="{789103A1-2331-4EA0-848B-CBAA69CCB213}"
s:ds="{BA9691BC-68D8-4816-9680-51BE64404362}" s:id="{52E40C93-F091-47C1-84DD-491DBCDC54B5}" s:par="ListSignature&PageSize&Offset&GridParameters=%7B908E2BC6-C110-4ED7-AF39-7EEACBB31A34%7D&FieldNames&Styles&TitleHeadingLevel=%7B6A44D9C6-61F0-4B69-8E1A-FDC27F96BA0F%7D&Reset Caching Options&RenderingIdentifier&DynamicPlaceholderId=1" s:ph="header" /></d></r>
So you can see that s:ds="local:/Data/MyEvents" is changed to s:ds="{BA9691BC-68D8-4816-9680-51BE64404362}"
Which script is run?
Following is the script run by this script action.
Import-Function Test-ItemIsPageData
Import-Function Edit-FieldValue
Import-Function Get-NestedDatasource
Import-Function Get-RelativeDatasourcePath
$item = Get-Item .
if ((Test-ItemIsPageData $item) -eq $true) {
$item = $item.Parent | Wrap-Item
}
$dataFolder = Get-ChildItem -Path $item.Paths.Path | ? { Test-ItemIsPageData $_ }
if ($dataFolder -ne $null) {
$dataSources = Get-NestedDatasource $dataFolder
$dataSourcesNames = Get-RelativeDatasourcePath $item
$dataSources | ? {
$ds = $_
($dataSourcesNames | ? { $ds.Paths.Path.EndsWith($_) }) -ne $null
} | % {
$dsItem = $_
$dsValue = $dataSourcesNames | ? { $dsItem.Paths.Path.EndsWith($_) } | Select-Object -First 1
$pattern = "ds=`"local:$($dsValue)`""
$replacement = "ds=`"$($dsItem.ID)`""
Edit-FieldValue $item "__Renderings" $pattern $replacement
Edit-FieldValue $item "__Final Renderings" $pattern $replacement
}
}
There is another script available "Convert data sources to local:". It works in reverse direction from the script "Convert data sources to GUID".