Import CSV Data in Sitecore Using PowerShell: Part-3

In my previous blog, we had discussed Sitecore PowerShell Command and Scripts. In this blog, I am discussing with you about How to Import CSV Data in Sitecore using PowerShell Scripts? To do the same you need to read the CSV file rows and create Sitecore Items into the Content Tree for each row then assign the column values of the row into the respective Item Field.

There are two options:

  1. Write your own customized PowerShell Script and Import CSV Data.
  2. Use the inbuilt Data Importer tool that comes with Sitecore PowerShell Extension

Option 1: Perform the Below steps to Import the CSV Data into Sitecore:

  1. First, you will need a CSV file. If you have Excel Sheet, then go to the File Menu and click on Save As option and change the format and save it into CSV format.
  2. Suppose you have a CSV file with the number of rows and each row has several columns with predefined values. Here I am taking the below CSV file for an example: Sitecore-Using-PowerShell-Part-3-1

    So, from the above CSV first row represent every Sitecore will have three fields named Title, Description, Summary.

    Note: Make sure that the header value of the column should not contain any space. For example, If the header is "Created Date" then it should be like "CreatedDate".

  3. The rest of the rows represent the Sitecore Items that will be created into the Content tree after successfully data import.

  4. Use the below PowerShell script to import CSV data into Sitecore Content Tree:
    #Define path you want to create sitecore Items
    $parentPath ="master:/sitecore/content/Helixbase/Models"
    #Read CSV file
    $importList = Import-CSV "C:\CSVFile\SampleCSVImport.csv"
    foreach ( $row in $importList ) {
    	$name = $row.Title
    	#Check if Title is not empty
    	if($name.Trim() -eq "")
    	{
    		write-host "Item name should not be blank: " $name  
            continue
    	}
    	$itemPath = $parentPath + "/" + $name #Create Item path
    	$currentItem = Get-Item -Path $itemPath -ErrorAction SilentlyContinue #Get the sitecore Item
    	if($currentItem -eq $null) #Check if Item is null then create new Item
    	{
    		try
    		{
    			$item = New-Item -Path $itemPath -ItemType "/sitecore/templates/Project/Helixbase/Page Types/Sample Page"                                
    			write-host "Item created: " $itemPath             
    		}             
    		catch
    		{
    			write-host "Failed to create Item: " $itemPath                
    			write-host $_.Exception.Message    
    			continue
    		}
    	}
    	#Assign Field values to the Sitecore Item  
    	$item.Editing.BeginEdit()
    	$item["Title"] = $row.Title
    	$item["Description"] = $row.Description
    	$item["Summary"] = $row.Summary
    	$item.Editing.EndEdit()
    }
  5. In the above code first, we are checking that the Item name i.e., Title is not blank. As we are going to create an item name by its Title Field. I have defined one variable ParentPath which shows the location where we are going to create the Sitecore Item. After that, we will check the item is already exist in our content tree or not. If it does not exist, then we will create a new item. At last, we will update/assign that Sitecore Item field values.
  6. After successful execution of the script, you can find your Sitecore items on a defined location in the ParentPath variable. See below image created Sitecore Items in my example: Sitecore-Using-PowerShell-Part-3-2

For more details about the Import CSV command, see the blog post: "PowerShell Command and Scripts for Sitecore Items: Part-2"

Option 2: Sitecore PowerShell Extension provides an inbuilt Data Importer tool to import CSV data. It gives you the three below listed options:

  1. Import and update items using a specified Template.
  2. Update existing items based on the item Id, item Name, or field name.
  3. Export existing items based on a specified Template.

To open the "Data Importer" window, go to the PowerShell Toolbox -> Data Management -> Data Importer and select the Data Importer option.

Sitecore-Using-PowerShell-Part-3-3

Once you select the option you will get Data Import Wizard with the below options and you can perform your desired functionality.

Sitecore-Using-PowerShell-Part-3-4

Happy Sitecoreing ðŸ˜Š

Comments

  1. Very useful, this is what I was searching else where and lately I found this great article

    ReplyDelete

Post a Comment

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Sitecore Technology MVP Journey 2022