Basic Sitecore Powershell commands

Get Item based on item id

Get-Item -Path “master:” -ID “{0DE95AE4-41AB-4D01-9EB0-67441B7C2425}

Get item id based on path

Get-Item Path “master:/sitecore/content/Sites/Site Data/ACP Components/Sample Data”

Get item properties

Get-Item -Path “master:/sitecore/content/Sites/ Site Data/Components Data/sample data” | select Name, Id, __Updated,__Created

Get item based on template name

Get-Item -Path “master:” -Query “/sitecore/content/Sites/Site Data/Components//*[@@templatename=’Component Parmaters’]”

Get item or items based on template id

Get-Item -Path “master:” -Query “/sitecore/content/Sites/Site Data/ACP Components//*[@@templateid=’{A3AEC2DA-31A8-46F8-ADB3-07697886321A}‘]”

Show list view or download the output with required properties or fields directly on the Item

Here Product is Sitecore bucket folder and items are created by code automation and we are only interested in the items with specific template ID

$allItems = Get-ChildItem “master:/sitecore/content/Sitecore/SiteA/Product/2017” -recurse | where-object {$_.TemplateID -eq “{42B5CFEF-AB2D-4E1F-A8D0-7DCD6A3A9DEH}”}
$allItems | Show-ListView -Property Name, Id, __Updated,__Created

Basics of Templates Creation in Sitecore

In general, templates basically should be defined under below categories at least. Templates are the most basic thing which is defined at the initial stage in Sitecore.

In general, templates basically should be defined under below categories at least.

  • Base templates
  • Data templates
  • Rendering parameter templates
  • Site configuration or settings templates
  • Page templates

Base templates as the name signifies that it should consists of the fields which is likely to be the part of most of the templates. For example- Title, Image and description and link fields. These fields can be part of many templates so instead of duplicating the same fields in all templates create it as a base template and inherit it.

Data templates are basically the Entities in the system. As we define entities in our code same way you can create Data templates and its fields. For example Product template in a ecommerce application.

Rendering parameter templates should be defined in a different folder and with rendering name as prefix so that one can get information of all the renderings for which rendering parameters exist.

Site configuration templates should be created in order to configure settings for Site specific data which is common on all pages. For example: enabling or disabling Banner and popups on the Site pages.

Page templates are the templates which might be inheriting data templates. Basically, these are the templates which will contain presentation (means renderings, placeholder and layouts) and are created based on number of website unique pages. For example: Page template might inherit Data templates, meta data templates.

Get single Item or multiple items in Sitecore based on different scenarios.

Get Item by ID:

Everything is an Item in sitecore and every item has an ID and Path.

If the return type of any method is Sitecore Item then we get all the properties and methods related to Item class.

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase(“master”);

string itmID={726C1CEB-46BC-483E-AAB4-44205C60773D}”);//Id of a sitecore Item
Item itmbyID=databas.GetItem(itmID);

Get Item by Path:

string itmPath=”/sitecore/content/home/ItemA”;

Item itmbyPath=databas.GetItem(itmPath);

Note: we should always try to get a item in sitecore by its Id not by its Path because path of any item can change by someone or accidentally(during Item move) but its item ID remains same.

Get Item using Sitecore query

Item item = Sitecore.Context.Database.SelectSingleItem("/sitecore/content/Site Data/Email Configuration");

Get Multiple Items using sitecore query

Item[] items = Sitecore.Context.Database.SelectItems("/sitecore/content/Site Data/Products//*[@@templateid='{B0707AB3-64C9-4CA1-AED4-3A1201208295}' ]");

Get multiple Items using Sitecore Fast query

Item[] items = Sitecore.Context.Database.SelectItems("fast://sitecore/content/Site Data/Products//*[@@templateid='{B0707AB3-64C9-4CA1-AED4-3A1201208295}']");

 Get all the descendants of the item using Sitecore API

Item item = Factory.GetDatabase(“web”).GetItem(“/sitecore/content/Site Data/Products”);
Item[] items=item.Axes.GetDescendants()

This method calls item.Children, and then for each child again child.Children recursively. And it adds all those items to an array.

There is another method which takes query as input as mentioned below:

item.Axes.Selectitems(string query) 

This method executes Sitecore query passed in the argument in the context of the current item

Get Descendents always fetch the child items recursively. It should be used carefully as it has huge performance hit if child items are large in size