Cooking with Sitecore Spotlights: Part 2

It’s your favorite Chef, back again! Last time, we were talking about Sitecore’s recommended use of a Sublayout’s Data Source field and how we use it to render spotlights. Today, I’m going to show you how to build on Sitecore’s blocks to make something more useful for your particular solution.

While it’s nice to be able to point to a single spotlight to render on your web site, usually you have more than one spotlight on each page. Using Sitecore’s solution, you would have to add a Spotlight sublayout control to the page for every spotlight you want to show. So, you would have to set the Placeholder for every spotlight, every Data Source for every spotlight, so on and so on. So we need a way to render a list of spotlights, not just one. To do that, set the Data Source property to the name of a treelist field on the item. For our example, I’m going to use the the Right Column Spotlights field.

Sitecore CMS Spotlights

The only new thing compared to the Sitecore recommended use is that you use the name of the field of spotlights you want to reference rather than the spotlight item you want to reference:

Sitecore Spotlights

Choose Your Coding Hierarchy

The code change is simple, too. The hierarchy of what to choose first is up to you – our solution checks for the specific spotlight first, followed by the data source field value. After looking for a specific item as the data source, we use the datasource value as a field name on the item and see if we have anything useful. Since ours is pointed to a treelist field, we are getting that field’s value of targeted items. If you are targeting a different field type such as a rich text field or a link field, you’ll need to accommodate your solution to correctly pull the data source field value.

if (Parent is Sublayout && !String.IsNullOrEmpty(((Sublayout)Parent).DataSource))
{
Item item = Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
if (item != null)
litText.Item = item;
else if (Sitecore.Context.Item.Fields[((Sublayout)Parent).DataSource] != null)
{
List items = new List();
Sitecore.Data.Fields.MultilistField multiListField = item.Fields[((Sublayout)Parent).DataSource];
foreach (Sitecore.Data.ID id in multiListField.TargetIDs)
{
Item spotlightItem = Sitecore.Context.Item.Database.GetItem(id.ToGuid().ToString());
if (spotlightItem != null)
items.Add(spotlightItem);
}
}
else
this.Visible = false;
else
this.Visible = false;

And there you have it. We’ve improved upon Sitecore’s recommendation of the use of the Sublayout DataSource field by adding a functional twist that makes our lives much easier.

This has quickly become one of my favorite recipes I’ve concocted so far here at Delphic. We’ve used it for pages that have spotlights in the left and right column while being able to use the same spotlight rendering control – simply by assigning what field to use as the spotlight source. We’ve also been able to use it in a pinch when the client has a new request but doesn’t want to wait until another code build to get it live.

« Prev Article
Next Article »