Sitecore has a very versatile and robust architecture, adding up to many functionalities, and giving results based on some changes, providing advantages in some or the other way.
Similar is the case with both these methods–GetItem() and SelectSingleItem() — about which we are to talk today!
Both the methods do the similar task — to fetch one item from the database.
Then why two methods if we can do the task by one method only?
Hence we started searching for understanding the same.
Sincere Thanks to Urvesh Vekariya and Kiran Patil to dig into this case, finding out various things and raising it to Sitecore Support.
Sitecore Support provided us a major help and an in-depth understanding of the case… Sincere thanks to Yuriy Yurkovsky
And the summary of our findings and reply from Sitecore Guys is as under:
What is that Method?
-The method is accessed as follows : Sitecore.Context.Database.GetItem() — 7 Overloaded types, I am talking about the one that takes string path as parameter
-The method is accessed as follows : Sitecore.Context.Database.SelectSingleItem() – Not overloaded, takes only one parameter string query
How it Works?
The GetItem() method just take the root item and tries to find the requested item among its children. If requested item isn’t found, it takes child item and search in its children and so on.
SelectSingleItem method uses the direct request to the database for the item.
What are the Pros and Cons?
-Faster in comparison with GetItem() as it directly reaches the specified item.
-Security Checks are not performed. So in this case, you got to check Security of this item if required using Item.Access Property
-Virtual Items can’t be returned.
-Not as fast as SelectSingleItem() as it has to move a number of items before it reaches the actual item being searched for, reason being the way of searching the item in a given tree.
-All the required Security Checks are performed.
-Virtual Items can also be returned.
For details on virtual item, please refer to the following links:
You may get a better performance with SelectSingleItem as compared to GetItem, at the cost of the disadvantages of Security checks (which you as a programmer can take care of using Item.Access Property) and virtual items.
Again, GetItem may be a little or more slower for returning items, but all the required security checks are performed and
Happy Understanding! 🙂