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