Using the Sitecore Links Database

Bhavik Patel • 3/25/2016

Sitecore’s Links database is actually a very useful tool when you are dealing with a lot of data and have to come up with creative ways to get information from Sitecore quickly.

In one of our recent encounters, we had to present a list of items that were related to the current item. Determining the related items could only be done via a “Treelist” field that existed in another item which was located in a different place in the content tree.

Unfortunately for us, it was difficult to locate this other item (that had the “Treelist” with the relations) via a pre-determined path, as often times, this item would be located in different places. So, our natural instincts brought us to writing a SelectItems() query with the appropriate “where” clause to locate the item we were looking for. It was something like this:

var item = Sitecore.Context.Item.Axes.SelectItems("//*[@@templateid='{the template id}' and contains(@relateditems, '{the current item id}')]"));

Essentially, the query above looked for all items that were created from a certain Template ID and checked the “relateditems” field to see if our current item existed in that field.

As you can imagine, the query was very slow in returning results. This was rightfully so, because Sitecore was essentially looking for a needle in a haystack. This is where the Sitecore links database would help.

For those who are not familiar with the Sitecore Links database, it is simply a table, usually stored in the “core” database, that maintains information about links between all Sitecore items. It’s how Sitecore can tell you that you are going to break links (and how many of them) when you delete an item. It’s also what powers the “Links” button in the Sitecore content editor ribbon, which provides you with information on which items refer to the currently selected item, and which items the currently selected item refer to. That’s to name a few. You can access the Links database through Sitecore’s APIs.

And so we did use the Links database to resolve our issue. Instead of querying other items to see if the current item was selected in one of the other items’ fields, we would check the links database to see which items the current item was referenced from and locate the specific one we were looking for. Example below:

var item = Sitecore.Globals.LinkDatabase.GetReferrers({current item})
 .Where(r => r.SourceFieldID == new Sitecore.Data.ID({relateditems field id}))
 .Select(i => i.GetSourceItem()).First();

Voila! This was extremely effective to get what we needed, in a fraction of the time the SelectItems() query took.

Bhavik Patel

Sitecore Technical Architect

Bhavik Patel is a certified Sitecore developer who has held Sitecore Most Valuable Professional (MVP) status since 2015. He has more than a decade of experience with enterprise-class Sitecore and Microsoft .NET framework development, including Sitecore integration with SharePoint and Business Intelligence solutions. He leads our Sitecore development practice, engineering enterprise-class Sitecore implementations for Government and commercial sector clients. He's an expert at using Sitecore Helix Framework to create re-usable architectures, streamline the integration of front-end development and provide the foundation for SEO-optimized Sitecore marketing websites.

Explore topics:

Other Blog Posts from Bhavik Patel