November 5, 2024

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?

1. GetItem()

-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

2. SelectSingleItem()

-The method is accessed as follows : Sitecore.Context.Database.SelectSingleItem() – Not overloaded, takes only one parameter string query

 

How it Works?

1. GetItem()

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.

2. SelectSingleItem()

SelectSingleItem method uses the direct request to the database for the item.

 

What are the Pros and Cons?

1. SelectSingleItem()

-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.

2.  GetItem()

-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:

http://sdn.sitecore.net/upload/sitecore6/64/reusing_and_sharing_data-a4.pdf

http://sdn.sitecore.net/Articles/Administration/Using%20Proxy%20Items%20in%205,-d-,3/What%20is%20a%20Proxy%20Item.aspx

 

Conclusion:

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! 🙂

1 thought on “Differences between SelectSingleItem and GetItem

  1. Hi,

    Nice comparisson, but the Sitecore.Context.Database.SelectSingleItem() in fact does security checks! By decompiling the code you will see a Access.CanRead() in the selectsingleitem method:

    public Item SelectSingleItem(string query)
    {
    Assert.ArgumentNotNullOrEmpty(query, “query”);
    bool processed;
    Item obj = this.DataManager.SelectSingleItem(query, out processed);
    if (!processed)
    obj = Sitecore.Data.Query.Query.SelectSingleItem(query, this);
    if (obj != null && obj.Access.CanRead())
    return obj;
    else
    return (Item) null;
    }

Leave a Reply

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