3

I am a Sitecore PowerShell newbie.

I have to write a Sitecore PowerShell script to ignore certain items from consideration based on their template types.

For this we can add these items to an array and use -notin for comparison.

Below is a code sample:

#ignored items list start
$nodeFolderTemplate = Get-Item -Path "/sitecore/templates/System/Node" 
$layoutFolderTemplate = Get-Item -Path "/sitecore/templates/System/Layout/Layout Folder" 
$rederingFolderTemplate = Get-Item -Path "/sitecore/templates/System/Layout/Renderings/Rendering Folder"
$commonFolderTemplate = Get-Item -Path "/sitecore/templates/Common/Folder"
$placeHolderSettingsFolder = Get-Item -Path "/sitecore/templates/System/Layout/Placeholder Settings Folder"
$branchFolderTemplate = Get-Item -Path "/sitecore/templates/System/Branches/Branch Folder"
$template = Get-Item -Path "/sitecore/templates/System/Templates/Template"
$dictionaryDomain = Get-Item -Path "/sitecore/templates/System/Dictionary/Dictionary Domain"
$dictionaryEntry = Get-Item -Path "/sitecore/templates/System/Dictionary/Dictionary entry"
$ignoredFolders = @( $nodeFolderTemplate.ID, $layoutFolderTemplate.ID, $rederingFolderTemplate.ID, $commonFolderTemplate.ID, $placeHolderSettingsFolder.ID, $branchFolderTemplate.ID )
$ignoredItems = @( $dictionaryDomain.ID, $ignoredFolders.ID, $commonFolderTemplate.ID)
#ignored items list end 

 $items = Get-ChildItem $selectedValues.SelectedFolderPath -Recurse | 
              Where-Object { $_.TemplateID -notin $ignoredFolders }

Can anybody please suggest a better way to do it?

5
  • And what is wrong with your current script?
    – Marek Musielak
    Commented Oct 11, 2019 at 9:28
  • She wants to know if there's an alternate approach to this. Commented Oct 11, 2019 at 9:33
  • It is working fine. But is there any better way than this?
    – NewbieHere
    Commented Oct 11, 2019 at 9:34
  • 2
    If that's a one time execution script and you don't plan to run it frequently, it's all good. If you want to execute it quite often in the future, you may think about improving performance a bit - e.g. check if removing -Recurse from the script and excluding some folders template early, then running get-children non recursively on the next level wouldn't be faster.
    – Marek Musielak
    Commented Oct 11, 2019 at 9:40
  • 1
    doc.sitecorepowershell.com/training Check this out. Commented Oct 11, 2019 at 13:15

1 Answer 1

3

The only issue I see with your script is that you are creating a variable for each template type. If you have a big list of templates then it would be difficult to manage the script.

So, Instead of creating a variable for each template path you can use an array for template paths.

Example -

$templatePaths  =     "/sitecore/templates/System/Node",
                      "/sitecore/templates/System/Layout/Layout Folder",
                      "/sitecore/templates/System/Layout/Renderings/Rendering Folder"

$templateIds = @()
$templatePaths | ForEach-Object -Process { $templateIds += (Get-Item $_ ).ID }

$items = Get-ChildItem $selectedValues -Recurse | Where-Object { $_.TemplateID -notin $templateIds }
1
  • Thanks! Exactly the thing I was looking for.
    – NewbieHere
    Commented Oct 11, 2019 at 12:06

Your Answer

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

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