I started working on a new Sitecore project recently, and I needed to have items for different lists like Countries, Cities, Universities, etc., to use it as lookup values, especially in Forms.
So I started by creating a new template to be used for those items. Something simple that holds key and value attributes.
For such small lists like Gender, you can add the items manually through content editors, but what about when you need lists like Countries, Nationalities, Universities, etc. You still can insert them manually, but where is the fun in that.
That’s when I decided to use SPE (Sitecore PowerShell Extensions) as I never had the chance to work with this module before.
I’m using Sitecore 9.3 in my project so I’ll go with the latest SPE version which is six as of this writing. Check the compatibility table for your instance here If you have never installed a Sitecore module before watch this short video tutorial.
Now that I have the module successfully installed on my Sitecore instance, it’s time to do some scripting, or that what I thought. Reading through the SPE documentation, I found that all that I need is there in the Toolbox.
Using Data Importer
When you have SPE installed, you will get SPE menu items when you open Sitecore in desktop mode.
The one I’m interested in is the Data Importer, clicking on that will open Data Import Wizard
The wizard has three modes to select. I’ll choose Import since this is what I’m trying to do.
When you click Next, Wizard will prompt you to import your CSV file. The one I want to import is a country list with code and name e.g., jo, Jordan. One note here it’s essential that you have one column named ‘Name’ which will be used for the Item name.
After you import the CVS file next wizard will let you set up the following:
- Destination Node: represents the parent node in the tree where new items should be created.
- Item Type: represents the Template used for new item creation and allows for automatic field matching.
- Field Name: Allows you to choose a field as a unique identifier for matching items instead of the Id.
I set the destination node and the template I’m using for such lookup items, and since this is a list of new items I don’t have yet on the tree, I’ll skip the Field Name.
Clicking Next, you’ll get a field mapping dialog where you can map the fields you have in CSV to the ones in the Item template.
Since I already have a $name token for Key field, it gets its value from the Item Name. I only have to map code to the value field in my template.
Give it some time, depending on the list size for the script to finish working.
When it’s done, you can see the execution result, whether it’s failed or succeeded.
It looks like I have the list successfully imported. Let’s check out the content tree.
All items now created under the parent I’ve chosen. Awesome!