Recently, we encountered an instance where we need to copy a
particular field value from Sitecore Web database to Master database in a
peculiar case.
I wrote a simple PowerShell script which did the trick. Here is the script and the same can be modified on the case to case basis. Here are the steps.
1.Filter and select the Sitecore items based on a template id for particular type of items from both master database and web database.
2. Loop through the list and find a matching item based on the item id from Master DB and Web DB. 3. Edit the match which common in both the database.
$pagetempId='{349F8A57-BA39-42DD-A82F-671641D05812}'
$path="/sitecore/content/MySite/Press/News"
$itemsmaster=Get-ChildItem master:$path -recurse | where-object { $_.'TemplateId' -eq $pagetempId}
$itemsweb=Get-ChildItem web:$path -recurse | where-object { $_.'TemplateId' -eq $pagetempId}
foreach($itemweb in $itemsweb)
{
foreach($itemmaster in $itemsmaster)
{
if($itemmaster.Id -eq $itemweb.Id)
{
#write-host 'master id: ' $itemmaster.Id "web id: " $itemweb.Id
#write-host 'master Title' $itemmaster.Title "web Title" $itemweb.Title
$itemmaster.Editing.BeginEdit()
$itemmaster["EventStartDate"] = $itemweb["EventStartDate"]
$itemmaster["EventEndDate"] = $itemweb["EventEndDate"]
$itemmaster.Editing.EndEdit() | Out-Null
#write-host 'master: Start ' $itemmaster.EventStartDate "web: start " $itemweb.EventStartDate
#write-host 'master: End' $itemmaster.EventEndDate "web: end " $itemweb.EventEndDate
break
}
}
}
The above script helped me to copy the field value EventStartDate and EventEndDate from Web DB to Master without much effort.
Please let me know if you have any queries or comments on this.
I wrote a simple PowerShell script which did the trick. Here is the script and the same can be modified on the case to case basis. Here are the steps.
1.Filter and select the Sitecore items based on a template id for particular type of items from both master database and web database.
2. Loop through the list and find a matching item based on the item id from Master DB and Web DB. 3. Edit the match which common in both the database.
$pagetempId='{349F8A57-BA39-42DD-A82F-671641D05812}'
$path="/sitecore/content/MySite/Press/News"
$itemsmaster=Get-ChildItem master:$path -recurse | where-object { $_.'TemplateId' -eq $pagetempId}
$itemsweb=Get-ChildItem web:$path -recurse | where-object { $_.'TemplateId' -eq $pagetempId}
foreach($itemweb in $itemsweb)
{
foreach($itemmaster in $itemsmaster)
{
if($itemmaster.Id -eq $itemweb.Id)
{
#write-host 'master id: ' $itemmaster.Id "web id: " $itemweb.Id
#write-host 'master Title' $itemmaster.Title "web Title" $itemweb.Title
$itemmaster.Editing.BeginEdit()
$itemmaster["EventStartDate"] = $itemweb["EventStartDate"]
$itemmaster["EventEndDate"] = $itemweb["EventEndDate"]
$itemmaster.Editing.EndEdit() | Out-Null
#write-host 'master: Start ' $itemmaster.EventStartDate "web: start " $itemweb.EventStartDate
#write-host 'master: End' $itemmaster.EventEndDate "web: end " $itemweb.EventEndDate
break
}
}
}
The above script helped me to copy the field value EventStartDate and EventEndDate from Web DB to Master without much effort.
Please let me know if you have any queries or comments on this.
Comments
Post a Comment