1

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?

3

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. enter image description here

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

enter image description here

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.