List of Sitecore API used frequently during development

Basic API Concepts and Retrieving Items

Important Sitecore Namespaces

  • Sitecore.Data - CRUD operations, item manipulations.
  • Sitecore.Context - Information about current request.
  • Sitecore.Links - Links Management

Useful in-built utilities

  • Sitecore.DateUtil
  • Sitecore.IO.FileUtil
  • Sitecore.StringUtil
  • Sitecore.UIUtil
  • Sitecore.MainUtil

Sitecore Context

It is a static class that contain information about installed Sitecore and current HTTP request-

  • Sitecore.Context.Database – Which database is being accessed
  • Sitecore.Context.Item – Which item is being viewed
  • Sitecore.Context.Language – What is the current Language
  • Sitecore.Context.User – Who is the current User

Retrieving Items

  • Items in sitecore are represented by Sitecore.Data.Items.Item
  • Items are retrieved by GetItem() method
    • Sitecore.Context.Database.GetItem("path_of_item");
    • Sitecore.Context.Database.GetItem("guid_of_item");
  • Do not hardcore the IDs or path is code in VS solution, try to put everything at one place (i.e. in a class file).
  • To get the Context Site use the below code –
    string startPath = Sitecore.Context.Site.StartPath;
    Item home = Sitecore.Context.Database.GetItem(startPath);
  • If you want to fetch the data from specific database use the below code – Sitecore.Configuration.Factory.GetDatabase("database_name"); // Either master or web
  • Use the same GetItem() method to get the Item –
    Database master = Factory.GetDatabase("master");
    Item=master.GetItem("/sitecore/content/home");
  • Use GetChildren() and .Children to get the collection of items of type Sitecore.Collections.ChildList. example,
    Sitecore.Collections.ChildList children = item.GetChildren();

Item Links

Sitecore’s ItemResolver resolves a URL to an item and can interpret various different URL formats.

Display Names allow alternative item names for different languages.

LinkManager and Item URLs

  • Always use the LinkManager to output an item’s URL
    • Use LinkManager.GetItemUrl() – recommended practice; SEO friendly URL
    • Do not use LinkManager.GetDynamicUrl() – URL with a GUID
  • Customize URL with UrlOptions
    • Language embedding, use .aspx show language sensitive Display name.
  • Or Globally if changing in Web.config
    • Override LinkManager completely
    • Then can change individual settings

Creating, Deleting and Modifying Items

 

3970cookie-checkList of Sitecore API used frequently during development

Add a Comment

Your email address will not be published. Required fields are marked *