# How to bulk-create template items automatically?

In Sitecore project, how would you bulk-create template items?

What we usually do is to write internal-design document, which defines Item Name, Item Path, Section Name, and Field Name in Excel Format, and convert it in XML format (using VBA!).

After uploading XML on Sitecore, we have custom module to import XML file and make it an item. In this way, we can make internal-design document as a record and also create item using the tool and Sitecore custom module.

However, I am thinking about making this process shorter because using tools to convert XML and uploading it on Sitecore, run a module is a bit too much work when we have tons of documents.

Any thoughts?

• Looks like something for Sitecore Powershell Extentions... Mar 20 '19 at 9:29
• You can write a Sitecore PS script to read the excel and do the rest of the stuff. doc.sitecorepowershell.com Mar 20 '19 at 9:51

Using the Sitecore Powershell Extension, you can easily create the templates without going through all this process.

# CSV Format

The below .csv file shows the entry of 1 template which contains 3 Sections.

When creating more than 1 template, the .csv will look like

Note, you can add new columns if you need to add datasources.

# Sitecore Powershell Script

Below is the script that you can execute to create the templates based on the .csv file. You'll need to change the value of the path for variable $importData. $importData = Import-CSV "C:\Path\to\the\csv\test.csv"

$templateSectionPath = "/sitecore/templates/System/Templates/Template section"$templateFieldPath = "/sitecore/templates/System/Templates/Template field"
$templatePath = "/sitecore/templates/System/Templates/Template" foreach ($row in $importData ) { Write-Host "Started for$($row.ItemName) with field name$($row.FieldName)"$section = $null$templateItemPath = "$($row.ItemPath)/$($row.ItemName)"

$item = Get-Item -Path master:$templateItemPath -ErrorAction SilentlyContinue

New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) {
if($item){$sectionPath = "$($item.Paths.FullPath)/$($row.SectionName)"
$sectionItem = Get-Item -Path master:$sectionPath -ErrorAction SilentlyContinue

if($sectionItem -eq$null){
$section = New-Item -Path$sectionPath -ItemType $templateSectionPath } else{$section = $sectionItem } } else{$templateItem = New-Item -Path $templateItemPath -ItemType$templatePath
$standardvalues = New-Item -Path$templateItem.Paths.FullPath -Name "__Standard Values" -ItemType $templateItem.Paths.FullPath$templateItem.Editing.BeginEdit()
$templateItem["__Standard values"] =$standardvalues.ID
$templateItem.Editing.EndEdit()$sectionPath = "$($templateItem.Paths.FullPath)/$($row.SectionName)"
$section = New-Item -Path$sectionPath -ItemType $templateSectionPath }$fieldPath = "$($section.Paths.FullPath)/$($row.FieldName)"
$field = New-Item -Path$fieldPath -ItemType $templateFieldPath$field.Editing.BeginEdit()
$field["Type"] =$row.FieldType
\$field.Editing.EndEdit() | Out-Null
}
}

Write-Host "------------------- Completed -------------------"


# Outcome

Below is a screenshot of the result

Note that you may need to adjust the script to accommodate your requirements. Examples, add Renderings, Insert Options etc.