Sitecore PowerShell Script To Upload ANY CSV Data

Are you looking for a quick and handy Sitecore PowerShell script to upload any kind of CSV data? Are you looking to create or update Sitecore items using PowerShell script? If so, then you landed on right blog post, keep on…

Many times, you require to create/update hundreds of Sitecore items in Sitecore which will take many hours of time if you decide to create/update those items manually.  Moreover, being a techie person it is smart decision to write some script or program to automate such thing and let machine do this bulk and tedious job for you. That’s why you are in field of programming, isn’t it? One such incident happen with my team recently where we required to create/update list of 200 countries in Sitecore CMS. We were provided the CSV file with Country name & Country ISO code and we were required to create a Sitecore item for each country resulting 200 items. Hence we decided to spin up some PowerShell script which will read data from CSV file and create Sitecore items accordingly.  A quick search to internet helped us with below Sitecore PowerShell script to do this job in seconds.

#It will create the folder if it is not found $dataFolder = [Sitecore.Configuration.Settings]::DataFolder $tempFolder = $dataFolder + "tempupload" $filePath = Receive-File -Path $tempFolder -overwrite  if($filePath -eq "cancel"){     exit }  $dropListPath =  "master:/sitecore/content/Master Countries/gigyacountryname"  # Get the item for the Dictionary Folder Container $dictionaryFolder = Get-Item -Path $dropListPath  # # Read CSV file # 1st row needs to have header of Key, Phrase. Other option is to specify the header on # the Import-CSV method call using the -Header string[] parameter #  $csv = Import-CSV $filePath -delimiter ","  # # Wrap the foreach loop in the BulkUPdateContext so we do not fire events and prevent indexing  #after individual create/update opertaion # New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {         foreach($row in $csv) {                 $name = SanitizeName $row.Name         $itemPath = $dropListPath + "/" + $name                 $item = Get-Item -Path $itemPath -ErrorAction SilentlyContinue                 #check is item already exits         if($item -eq $null)         {             Write-Host ("Creating New Item at: " +  $itemPath)             Write-Host ("Name:" + $name)             try             {                 $item = New-Item -Path $itemPath -ItemType "/sitecore/templates/Project/CountryDropDown"                                Write-Host "Item created: " $item.Name             }             catch{                 Write-Host "Failed to create Item: " $item.Name                 Write-Host $_.Exception.Message             }         }         else{             Write-Host ("Updating Item:" + $item.Name)         }         $item.Editing.BeginEdit()         $item["Name"] = $row.Name         $item["Value"] = $row.Value         $item.Editing.EndEdit() | Out-Null          # we don't want to send the output to the console otherwise it will print False in the console.         Add-ItemLanguage -Path $item.Paths.Path -Language "en" -TargetLanguage          "ar-AE" -IfExist OverwriteLatest  -IgnoredFields ""     } }  # # Clean the name in case it has restricted characters # Todo: use Sitecore.Data.Items.ItemUtil.ProposeValidItemName($args[0]) # function SanitizeName{  $args[0].Replace(".", "").Replace(" ", "").Replace("/", "").Replace("-", ""). Replace("&", "").Replace(":", "").Replace("""", "").Replace("#", "")  } 

Steps:

  1. Go to Sitecore PowerShell ISE > Copy/Paste above script. 
  2. This will open file dialog > Select CSV file

Below is the CSV format which we were using for above PowerShell Script.

Hope this will help someone!

Leave a Reply