Sunday 16 November 2014

Sitecore API

The Sitecore API is very powerful; you are able to query Sitecore items using a number of different technologies, from standard .NET web forms to using XSLT and MVC. Sitecore items can also be queried using a number of methods; they have their own query languages Sitecore Query and Sitecore Fast Query which are very similar to XPath, and you are also able to use Linq to query Sitecore items and use ‘where’ clauses to filter down further.

To access any Sitecore item you can use the Sitecore.Data.Items.Item. Sitecore provides specialized classes to represent specific types as items, such as Sitecore.Data.Items.TemplateItem to represent a data template and Sitecore.Data.Items.MediaItem to represent a media item.

GetItem: The Sitecore.Data.Database.GetItem() method use to retrieve a Sitecore.Data.Item.Item. which take the ID of the item or the path to the item as the first parameter to the Sitecore.Data.Database.GetItem()

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");
Sitecore.Data.Items.Item mediaRoot = master.GetItem(Sitecore.ItemIDs.MediaLibraryRoot);
Sitecore.Data.Items.TemplateItem standard = master.Templates[Sitecore.TemplateIDs.StandardTemplate];

Editing the item: Place an Item in Editing Mode before edit using Sitecore API
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home"); 

using (new Sitecore.SecurityModel.SecurityDisabler()) 
home.Editing.BeginEdit(); 
try { 
//TODO: update home 
home.Editing.EndEdit(); 
catch (Exception ex) 
home.Editing.CancelEdit(); 
}

If we do call the Sitecore.Data.Items.Item.Editing.CancelEdit() method or do not call the Sitecore.Data.Items.Item.Editing.EndEdit() method, Sitecore does not commit the changes.

Get Children of an Item: Use the Sitecore.Data.Items.Children property to access the children of an item. For example, to access the children of the /Sitecore/Content/Home item in the Master database: Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home"); foreach(Sitecore.Data.Items.Item child in home.Children) 
{
 //TODO: process child
}

Get Descendant of that item: Using a recursive method we can access descendant of the items.
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home"); ProcessRecursively(home); 
... 
private void ProcessRecursively(Sitecore.Data.Items.Item item) 
{
//TODO: process item 
foreach(Sitecore.Data.Items.Item child in item.Children) 
     ProcessRecursively(child);
}
}

Get Parent of an Item: You can use the Sitecore.Data.Items.Item.Parent property to access the parent of an item.
Sitecore.Data.Items.Item parent = home.Parent;

Get Ancestors of an Item: Can use the Sitecore.Data.Items.Item.Parent property in a recursive method to access the ancestors of an item.
ProcessRecursively(home.Parent); 
... 
private void ProcessRecursively(Sitecore.Data.Items.Item item) 
//TODO: process item 
if (item.Parent != null ) 
   ProcessRecursively(item.Parent);
}

Sitecore Query: Can use the Sitecore.Data.Database.SelectItems() method to retrieve items in a database that match a Sitecore query.
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); 
string query = String.Format("//*[@@templateid='{0}']", Sitecore.TemplateIDs.Folder); Sitecore.Data.Items.Item[] queried = master.SelectItems(query);

Other Operations

Rename an Item :
Sitecore.Data.Items.Item sample = master.GetItem("/sitecore/content/home/sample"); sample.Editing.BeginEdit(); 
sample.Name = "Changed"; 
sample.Editing.EndEdit();

Move an Item :
Sitecore.Data.Items.Item sample = master.GetItem("/sitecore/content/home/sample"); Sitecore.Data.Items.Item content = master.GetItem(Sitecore.ItemIDs.ContentRoot); sample.MoveTo(content);

Copy an Item :
Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home"); 
Sitecore.Data.Items.Item content = master.GetItem(Sitecore.ItemIDs.ContentRoot); home.CopyTo(content,"Sibling");

Delete an Item :
Sitecore.Data.Items.Item sample = master.GetItem("/sitecore/content/home/sample"); sample.Delete();
Delete the Descendants of an Item :
You can use the Sitecore.Data.Items.Item.DeleteChildren() method to delete the descendants of an item.


3 comments: