2

I have created sitecore template using powershell extension. Here is my powershell script:

$template = New-Item  -Path "/sitecore/templates/PowershellTask" -Name "testtemplate" -type "{AB86861A-6030-46C5-B394-E8F99E8B87DB}"

$standardvalues = New-Item -Parent $item -Name "__Standard Values" -type $template.ID
$template.Editing.BeginEdit()
$template["__Standard values"] = $standardvalues.ID
$template.Editing.EndEdit()

I don't know how to add fields to this template. Can anyone help me out with this?

1 Answer 1

7

I wrote this a while back. The script includes a helper function for creating new templates. Feel free to modify this to include adding Standard Values.

function New-TemplateItem {
    param(    
        [ValidateNotNullOrEmpty()]    
        [string]$Path,    
        [string]$Icon,   
        [string]$BaseTemplate,    
        [PSCustomObject[]]$Fields    
    )

    $itemTemplate = New-Item -Path $Path -ItemType "/sitecore/templates/System/Templates/Template"    
    $itemTemplate."__Base Template" = "{1930BBEB-7805-471A-A3BE-4858AC7CF696}"

    if($BaseTemplate) {    
        $itemTemplate."__Base Template" = $BaseTemplate    
    }

    if($Icon) {    
        $itemTemplate."__Icon" = $Icon    
    }   
        
    if($Fields) {    
        foreach($field in $fields) {    
            $sectionPath = "$($Path)/Data"    
            if($field.Section) {    
                $sectionPath = "$($Path)/$($field.Section)"    
            }

            if(-not(Test-Path -Path $sectionPath)) {    
                New-Item -Path $sectionPath -ItemType "/sitecore/templates/System/Templates/Template section" | Out-Null    
            }

            $fieldItem = New-Item -Path "$($sectionPath)/$($field.Name)" -ItemType "/sitecore/templates/System/Templates/Template field"    
            $fieldItem.BeginEdit() | Out-Null    
            $fieldItem["Type"] = $field.Type    
            $fieldItem["Title"] = $field.Title    
            $fieldItem["Source"] = $field.Source    
            $fieldItem.EndEdit() | Out-Null    
        }    
    }    
}    

$fields = @(    
    [PSCustomObject]@{    
        "Name" = "Artist"    
        "Title" = "Artist Title"    
        "Type" = "Single-Line Text"    
        "Section" = "Details"    
    },    
    [PSCustomObject]@{    
        "Name" = "Description"    
        "Title" = "Artist Description"    
        "Type" = "Rich Text"    
        "Section" = "Details"    
        "Source" = "/sitecore/system/Settings/Html Editor Profiles/Rich Text Default"    
    }    
)    

$templatePath = "master:/sitecore/templates/User Defined/SetList"    
$baseTemplateIds = "{1930BBEB-7805-471A-A3BE-4858AC7CF696}"    
Remove-Item -Path $templatePath -Recurse    
New-TemplateItem -Path $templatePath -Icon "office/32x32/earth_music.png" -BaseTemplate $baseTemplateIds -Fields $fields
2
  • Can u help me out with assigning source value to droplink field in sitecore template. ` DropLink{ $spiltedValue=$importData.DropLink -split ","; if(!([string]::IsNullOrEmpty($spiltedValue))){ foreach ( $fieldName in $spiltedValue ) { $field=New-Item -Parent $sectionValue -Name $fieldName -type "{455A3E98-A627-4B40-8035-E683A0331AC7}" $field.Type="DropLink" $field.**??**="/sitecore/content/ListManagerTask" } } break }`Don't know how to give path? Jul 8, 2019 at 7:00
  • Perhaps this article explains what possible values can go in the source field. sitecore.wonderamy.com/2011/03/using-source-property.html Jul 8, 2019 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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