More in this section
Categories
Bloggers
Blogs RSS feed

Customizing Sitefinity SearchResults - displaying Product thumbnails and other Content type-specific data

by User Not Found
Many of you have requested assistance in delivering various custom functionality in the Sitefinity SearchResults widgets.

From displaying the Page's description, showing custom messages depending on the type of the item, to displaying Product thumbnails.

By default the SearchResults widget operates with an IResultSet containing objects of type IDocument - these are the item representations in the Sitefinity search index, and are the same for all Sitefinity content, thus only general properties can be persisted.

Each IDocument carries the information for the item's ContentType  in the contain the "ContentType" field where you can obtain the correct information what kind of item you're dealing with.

Once you've resolved the type, you can then operate with this type's specific properties, however you first need to get the actual item (remember what you've been working with so far is the Lucene document, not the Sitefinity item). 

This is done by obtaining the item's ID. By default the field name where the original Sitefinity content ID is stored inside each Lucene document varies depending on how the particular module has configured its inbound pipe. The unified concept that we have adopted is that all Content types will use the "OriginalItemId" field.

Once we have information about the ContentType  and the OriginalItemId this allows us to determine which content manager we need to call, and which Id to pass, in order to retrieve the actual item. Form then on operating with the item is just as with any item of that type - you can get its properties and use them depending on your specific use case scenario.

So for the SearchResults widget we can easily create an external template based on the default one, that  will allow us to use code-behind, subscribe to the ResultsListItemDataBound  event in the template code-behind and do something like:

using System;
using System.Linq;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Ecommerce.Catalog.Model;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Ecommerce.Catalog;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Utilities.TypeConverters;
using DotNetImageControl = System.Web.UI.WebControls.Image;
 
namespace SitefinityWebApp.CustomTemplates
{
    public partial class SearchResultsProducts : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.resultsList.ItemDataBound += ResultsListItemDataBound;
            this.catalogManager = CatalogManager.GetManager();
            this.libMan = LibrariesManager.GetManager();
            this.pageMan = PageManager.GetManager();
        }
 
        void ResultsListItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var myitem = e.Item.DataItem as IDocument;
 
            if (myitem != null)
            {
                //You need to check for the type of the item you have gotten in your search results
                var type = myitem.GetValue("ContentType");
 
                //Documents enter the index with an Id field
                if (TypeResolutionService.ResolveType(type) == typeof(Document))
                {
                    var docItem = libMan.GetDocument(new Guid(myitem.GetValue("Id")));
                    var path = docItem.FilePath;
                }
 
                //Pages get in the index with OriginalItemId field
                //Here's how you can display a page property in your search results
                if (TypeResolutionService.ResolveType(type) == typeof(PageNode))
                {
                    var ID = new Guid(myitem.GetValue("OriginalItemId"));
                    //you can now get the item with its manager and access its properties,
                    //since you'll be working with an object of the correct type
                    //for example:
                    var page = pageMan.GetPageNode(ID);
                    if (page != null)
                    {
                        //check if the PageNode has PageData
                        if (page.Page != null)
                        {
                            //now you can simply get a control from your template and set the value to it,
                            //so it can be displayed
                            var literalControl = e.Item.FindControl("pageDescriptionLiteral") as Literal;
                            literalControl.Text = page.Page.Description;
                        }
                    }
                }
 
                //Products, and Content items also enter the index with OriginalItemId field
                if (TypeResolutionService.ResolveType(type) == typeof(Product))
                {
                    var ID = new Guid(myitem.GetValue("OriginalItemId"));
                    //get the control that will display our data on the template
                    var image = e.Item.FindControl("productThumbnail") as DotNetImageControl;
                    //get the actual Product item
                    var product = CatalogManager.GetManager().GetProduct(ID);
                    if (product != null)
                    {
                        //ste the property value to the control so it will be displayed on the frontend
                        image.ImageUrl = product.PrimaryImageUrl;
                    }
                }
            }
 
        }
 
        private CatalogManager catalogManager;
        private LibrariesManager libMan;
        private PageManager pageMan;
    }
}

 

For your convenience I'm attaching an archive of the control template above to this post.

In conclusion, you've probably realized that this post just scratches the surface on the amount of modifications that can be done when customizing your search results, hope it helps you achieve your desired functionality.

11 comments

Leave a comment
  1. Justin Feb 05, 2013
    I have been trying to get this functionality into our search results, however I seem to have missed something as I receive this error message:

    Exception Details: System.ArgumentException: There is no field with the specified name.
    Source Error:
    Line 34: var productItem = catalogManager.GetProduct(myitem.GetValue("OriginalContentId"));

    we are running this version of Sitefinity: Sitefinity 5.2.3800.0

    Any help would be very grateful.





  2. Boyan Feb 21, 2013

    Hey Justin,

    Thank you for letting us know of the problem with he provided sample. The post information and the sample have been updated to properly reflect the correct property name.

  3. Vishika Jan 14, 2014
    Hello I have tried your code above and I have an error message as below:
    Error parsing the template

    I am actually using Sitefinity 6.3.  I would be grateful if you can help me out of it please

    Thank you
    Vishika
  4. بهترین وکیل ملکی Apr 11, 2018
    Whoah this weblog is great i love studying your articles. Keep up the great work! You know, many people are searching around for this info, you could aid them greatly. بهترین وکیل ملکی
  5. website Apr 11, 2018
    you have a very excellent weblog here! if you’d like to make some invite posts in my small weblog? website
  6. Horeca Koeling Apr 16, 2018
    Precisely  what I was  searching  for,  appreciate it for  putting up. Horeca Koeling
  7. mamae sarada vale a pena Apr 16, 2018
    Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated. mamae sarada vale a pena
  8. air data tester Apr 17, 2018
    There are certainly many details like this to take into consideration. It really is a excellent specify bring up. I provide the thoughts above as general inspiration but clearly there are actually questions just like the one you bring up where the most important factor might be doing work in honest good faith. I don?t know if best practices have emerged around items like that, but I know that the job is clearly referred to as a good game. Both boys and girls notice the impact of just a moment’s pleasure, throughout their lives. air data tester
  9. online courses May 05, 2018
    you can buy some promise rings from ebay but those are the cheap ones, the quality ones are sold elswhere,, online courses
  10. Lucky patcher May 07, 2018
    There are a handful of intriguing points with time here but I do not know if I see these people center to heart. There is certainly some validity but I’ll take hold opinion until I take a look at it further. Good post , thanks and we want a lot more! Added to FeedBurner also Lucky patcher
  11. online audio May 07, 2018
    Thank you for this kind of information I was looking all Google to be able to discover it! online audio

    Leave a comment