Skip to main content

Copy field value from Web database to Master database using Powershell

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.

Comments

Popular posts from this blog

Sitecore 9.3 Installation | Common Issues and Resolutions

Recently, I have installed and setup Sitecore 9.3 on another machine. I faced similar/same kind of issues which I faced earlier. Thought, it would be helpful to the Sitecore Community if I capture and document them along with resolution steps I which took and be successful in settting up. Here are few of them I could document them. 1. Failed to start service 'Sitecore Marketing Automation Engine - sitecore93xconnect.dev.local-MarketingAutomationService (sitecore93xconnect.dev.local-MarketingAutomationService)'. Solution : This issue occurs because of non-self-signed certificates, hence please follow the below steps to resolve your issue. Execute below PowerShell script to find out if there are any non-self-signed certificates: 1. Open the PowerShell Console (Admin mode recommended) 2. Execute below command Get-Childitem cert:\LocalMachine\root -Recurse | Where-Object {$_.Issuer -ne $_.Subject} 3. If it shows any results listing any non-self-signed certificates...

Steps to create a Sitecore JSS app and Deploy to Sitecore

If you are a beginner in Sitecore JSS and you are looking step-by-step by process to setup your first JSS App with Sitecore, please proceed further and setup up your first JSS App along with deploying it to Sitecore. Before we jump start creating JSS APP, let us start with a quick introduction to Sitecore JSS. What is Sitecore JSS?  Sitecore JavaScript Services (JSS) is a complete SDK for JavaScript developers that enables you to build full-fledged solutions using Sitecore and modern JavaScript UI libraries and frameworks. Build Headless JavaScript applications with the power of Sitecore. Use your favorite JavaScript framework powered by an enterprise experience platform like React, Angular or Vue.js. Prerequisites for Connected mode with Sitecore Required Installations node.js v6.9 or later npm v5.7 or later Sitecore 9 or later installation with JSS(For JSS component ref, https://dev.sitecore.net/Downloads/Sitecore_JavaScript_Services.aspx) Required Settings  ...

Sitecore JSS - Custom Rendering Content Resolvers

In Sitecore, while working for real-time projects, you may get scenarios where you would see a need to customize you rendering output to serialze. Sitecore provides three different ways of customizing your rendering output with Sitecore JSS. They are as below. • Use JSS GraphQL Support - Using Integrated GraphQL queries • Use Out-of-the-box Rendering Content Resolvers - Using Sitecore JSS built-in Rendering Content Resolvers • Implementing a Custom Rendering Content Resolvers by inheriting RenderingContentsResolver Why Customization? In Sitecore development, many times we encounter scenarios to deal with hierarchical item structures like multi-level menu, product category, product sub-category, product details, forms etc., and even, in case where there is a need to combine the Sitecore data with any non-Sitecore data. In these types, Sitecore JSS is not able to serialize item structures with sub-hierarchies into JSON. Sitecore Jss allow us custiomize in...