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.


Sitecore Web Service

The Sitecore service layer is based on web services. It is useful when the developer wants to use Sitecore as a repository for his site content.

Sitecore provides two sets of web services:

  • Visual Sitecore Service (Good Old Web Service) at /sitecore/shell/WebService/service.asmx
  • Sitecore Web Service 2 (Hard Rock Web Service) at /sitecore/shell/WebService/service2.asmx
























Some of the methods purposes are:

Method Name
Purpose
AddFromMaster
To create and item under master item.
AddFromTemplate
To create and item of specified template type.
AddVersion
To create new version of item.
CopyTo
To create copy of existing item.
Delete
To delete and item.
DeleteChildren
To delete children of the specified item.
GetChildren
To retrieve children of item.
GetDatabases
To return list of the database names .
GetItemFields
To returns a list of the fields that this item contains.
GetItemMasters
To return master of the item
GetLanguages
To returns a list of the languages that are set for the entire site.
GetTemplates
To returns a list of the templates in the site.
GetXML
To returns the XML representation of an item.
MoveTo
To move the item under different parent
Rename
To Rename an item
InsertXML
To insert item based on representation
Save
To updates the item based on the representation
VerifyCredentials
To validate user authorization.

Sitecore Databases

Sitecore is 100% Microsoft based, SQL Server, .NET Framework 3.5, and IIS is supported. Sitecore data is stored in multiple databases and itself is made up of three databases and a web application. Sitecore has three default databases and they are core, master and web.

Databases serve following purpose:

Core database :

The core database is used by Sitecore to manage membership, handle system settings and hold the entire configuration for all applications in the CMS. This database can be accessed through the CMS, and you are able to configure any of the settings, override standard functionality, build your own applications, and add buttons and functionality to the ribbon in the editors. The managed website that provides the Sitecore user interfaces accesses the core database and allows the user to view and edit content in the master database by default.

Master database :

The Master database is the authoring database - it contains all versions of any content or assets. CMS user interfaces access the master database by default. The master database is where all the content editor work is done. Whenever a new piece of content is created, edited or deleted it is stored here, including those in preview mode. The master database can have workflow enabled and customized, so sections of the site can be locked down to certain roles, or made to go through an approver or translator before they go live.


Web database :

The web database is the default publishing target database: by default, Sitecore publishes changes from the master database to the web database. It differs from the master database in that it doesn’t contain anything in preview mode and only stores the latest live version of each item. When a content editor publishes some content it is then copied from the master database to the web database.


Access and context:

Databases can be accessed through the Database class. 
To get a reference to a database, use the Factory class

Sitecore.Data.Database master =   Sitecore.Configuration.Factory.GetDatabase("master");

Whenever the user code is invoked by Sitecore, a so called context database is automatically assigned. You can access this database by using the Context class:

  Sitecore.Data.Database current = Sitecore.Context.Database;

A Sitecore database contains data elements called items. Programmatically, the data represented by Items is accessed through the class Item.

  To get an Item from the current context database, you can use

  string itemPath = "/sitecore/content/home"; 
  Sitecore.Data.Items.Item item = Sitecore.Context.Database.Items[itemPath];

The itemPath is the path of Item names leading to a specific Item.
Items can be retrieved using paths or IDs. Languages and versions can also be specified

  Sitecore.Data.ID itemID = Sitecore.Data.ID.Parse( 
    "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"); 

  Sitecore.Globalization.Language language =  
    Sitecore.Globalization.Language.Predefined.English;
  Sitecore.Data.Version version = Sitecore.Data.Version.Parse(1);  
  Sitecore.Data.Items.Item homeitemlanguage =  
    Sitecore.Context.Database.Items[itemID, language, version];

The data within an Item is organized in named fields. For instance, an Item to be displayed on a web site may contain a title and some text. To access the values of these fields, you can use an indexer on an Item

Sitecore.Data.Items.Item homeitem = Sitecore.Context.Database.Items["/sitecore/content/home"];
string title = item["title"];
string text = item["text"]; 

Sitecore Content Tree

Content Tree:


The Content Tree allows viewing the Sitecore system as a tree with child items grouped under their parents. The Content Tree Structure is displayed by default and contains the hierarchy of folders and content records comprising the site. All the content contained in a Sitecore web application is stored in the content tree, with the Content and Media Library items intended for content editors’ use and the Layouts, System and Templates folders aimed at developers and system administrators. Sitecore considers everything within the content tree an item that can be queried using the API; so all pages are items, as are images, PDFs and so on, meaning they can be easily found using this method.





Top items folders in content tree are:

Content :

The Content item folder is where the pages and data for the website are stored, and the structure of these items represents the structure of the website.

Media Library:

The Media Library, accessed with the Media Browser, is used to manage media files (images, PDFs, documents, audio, video, and multimedia) are stored in the Media Library.

Layout :

The Layout item folder is where all the ‘layouts’ for the web application are declared, referencing the location of the file in the web application root. It also contains ‘sublayouts’; partial files that represent the widgets etc. used to create a page. Layouts are combined with multiple sublayouts to build pages, which means widgets can be reused and content editors can swap out one widget for another without developer intervention.

System :

The System item folder is where standard settings for the content editor and the web application are held. For example, if you wanted to add another language to the web application this is where you would do it.

Templates :

The Templates item folder is a where all the template items are stored for the site. Template items are used to describe objects in Sitecore, and specify what fields that object will contain, the content of any standard values and any layouts that are attached to the item

Sitecore Overview


Sitecore is a one of the leading enterprise-level .NET web content management software. Sitecore gives mid to large size organizations a comprehensive tool with the ability to build, deploy and securely manage business solutions such as web applications, public websites which require CMS (Content Management System) functionality, integration options and scalability.

CMS is a web application designed to make it easy for non-technical users to add, edit and manage a website contents. Some of important features of a CMS like Web-based publishing, format management, revision control, indexing, search, and retrieval.

KEY FEATURES & BENEFITS

Sitecore architecture: Sitecore architecture is nice plug-gable and scalable component based architecture that can essentially be selected, assembled and syndicated across multiple sites. It is written in .Net.

Simple and powerful interface: Sitecore has a powerful and simple end-user interface for content manager as well as for developer. User will feel familiar with interface if he/she has used MS office earlier.

Cutting Edge Technology: Sitecore is completely built on Microsoft .NET technologies, so sites built with Sitecore integration will run in any browser and on any browser-based device. The Sitecore user interface works in Internet Explorer, Mozilla, Firefox and Mac browser, to mobile devices such as iPhone, Android, and Windows Phones,. Dynamic delivery, intelligent caching and ASP .Net delivers pages very quickly. The product is compliant with the Worldwide Consortiums open Internet standards, for example SOAP (Web services), XML and uses the extensible application markup language (XAML) concept.

External Data Integration: Sitecore CMS provides a full data integration and abstraction layer ( Sitecore Data Providers/Importer) that will allow you to connect to any database, Web service or other external systems and access that content.

Improved Search Engine Optimization: Sitecore created the SEO module to improve sites search engine ranking through that not only helps increase Google ranking, but also improve the search experience for visitors of website.

Easily Integrate with Third Party Tools: Sitecore seamlessly integrates with third party tools such as SharePoint, various ERP and CRM systems, Clay Tablet, Google Analytics and many more modules.

Deliver personalized content based on user behavior: Content personalization through Sitecore gives your marketing team the ability to own your customer experience in a much tailored method.

Content management workflow: Sitecore's workflow engine includes versioning, controlled approvals, incremental publishing, notifications and reminders, and archiving capabilities. The document comparison feature allows enhanced version control content, allowing editors and approvers to see exactly what has changed. This feature is also useful when managing content translation processes, highlighting exactly what content has changed and needs to be translated from its source language. Having strong workflow processes ensure your content is the highest quality it can be, and your site delivers the most compelling user experience it can.

Multi-language content support: As more companies expand their reach globally, Sitecore allows editors to work with the CMS in their native languages and provide multi-lingual capabilities based on diverse user audiences. Sitecore also lets you to coordinate the management of the many language versions of your website, as well as the translation processes as content changes.

Rapid Development: Sitecore lets you use the tools you want to use and provides unparalleled support for .NET developers. You can work directly in Sitecore or use external tools such as Microsoft Visual Studio.

Searching: Sitecore exposed search API for CMS item searching. Developer can also easily integrate open source search technology Lucene with CMS. sitecore also provide fast query syntax for faster search.

Security: Sitecore offers an incredible level of security, enabling you to manage content down to their component and element levels. It also provides a sophisticated permission management system to grant rights to users, groups and roles. Sitecore also supports external authentication and authorization systems, and was engineered to plug into existing systems such as CRM solutions to power visitor authentication as well as easily integrating into Active Directory for user management.