+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom Search Results

Custom Search Results

44 posts, 1 answered
  1. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    07 Apr 2010
    Link to this post
    I need to create an override of the search results control to limit the results based on the page group the current visitor is under.

    I have read many/all of the post regarding the override search code, but am lost as to where to put this code. I am also lost on how to get the search results control, or a new custom search results control, to read from the override method.

    If you could create a sample project that would be awesome.

    PLEASE DO NOT send me to any other forum threads, I have read them all and I am STILL LOST.

    Thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Apr 2010
    Link to this post
    Hello mcpdinkansas,

    You have to override CreateChildControls of your custom SearchResults control where you can create a new list of ResultItems which will be used as a data source for the Repeater control that shows all results. You could add the page ID to the snippet of theSearchResult so that you could get the current page from ICmsUrlContext and check whether it is group or not.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    08 Apr 2010
    Link to this post
    Hey Ivan,

    Where would I place the code for the override?

    How do I point the search results control to use my override method?

    When creating the override method, these lines of code:

    this.layoutCnt.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);

    this.layoutCnt.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);

     newList.Add(customItem);

    produce an error. What using statement or class do these use?

    Here is my basic override code as of now:

    public class CustomSearchResultsControl : SearchResult
        {
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                this.layoutCnt.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.layoutCnt.ResultsList.DataBind();
     
                this.Controls.Clear();
     
                this.layoutCnt = new Container(this);
                this.LayoutTemplate.InstantiateIn(layoutCnt);
     
                if (!string.IsNullOrEmpty(Query))
                {
                    string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
                    string nPath = PathUtil.GetIndexPhysicalPath(CustomCatalogName);
                    if (Directory.Exists(vPath) && Directory.Exists(nPath))
                    {
                        // check if query string is supplied to select the current page
                        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
                        {
                            this.CurrentPage = Convert.ToInt32(
                                HttpContext.Current.Request.QueryString[PageKey]);
                        }
     
                        string searchQuery = Query;
                        string message = string.Empty;
     
                        bool isValid = this.EscapeSpecialChars ? this.EscapeSpecialChars : ValidateQuery(ref searchQuery, out message);
     
                        if (isValid)
                        {
                            int totalItems;
                            int totalItems2;
                            int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
     
                            try
                            {
     
                                IList<ResultItem> CustomDataSource = SearchManager.Search(
                                       searchQuery,
                                       NewCatalogName,
                                       startIndex,
                                       this.PostsPerPage,
                                       this.WordsMode,
                                       this.EscapeSpecialChars,
                                       out totalItems2);
     
     
                                foreach (ResultItem customItem in CustomDataSource)
                                {
                                    newList.Add(customItem);
                                }
     
     
                                totalItems = totalItems + totalItems2;
     
                                int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
     
                                if (numberOfPages == 0 && totalItems > 0)
                                    numberOfPages = 1;
     
                                string qeryTest = this.Query.Trim('\"');
                                ((Control)this.layoutCnt.ResultsStats).EnableViewState = false;
                                this.layoutCnt.ResultsStats.Text = string.Format(this.layoutCnt.ResultsStats.Text, totalItems, qeryTest);
     
                                if (this.AllowPaging)
                                {
                                    this.layoutCnt.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);
                                    this.layoutCnt.Pager1.PageCount = numberOfPages;
                                    this.layoutCnt.Pager1.SelectedPage = this.CurrentPage;
                                }
                                this.layoutCnt.ResultsList.DataSource = CustomDataSource;
     
                            }
                            catch (Telerik.Lucene.Net.QueryParsers.ParseException ex)
                            {
                                this.layoutCnt.ResultsStats.Text = "strange chars";
                                Log.Exception(ex);
                            }
                        }
                        else
                        {
                            this.layoutCnt.ResultsStats.Text = message;
                        }
                    }
                    else
                    {
                        this.layoutCnt.ResultsStats.Text = String.Format("InvalidIndex", this.IndexCatalogue);
                    }
                }
                else
                {
                    this.layoutCnt.ResultsStats.Text = String.Empty;
                }
     
                this.layoutCnt.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.layoutCnt.ResultsList.SkinID = this.SkinID;
     
                Controls.Add(this.layoutCnt);
                this.layoutCnt.ResultsList.DataBind();
     
            }
     
     
        }
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Apr 2010
    Link to this post
    Hi mcpdinkansas,

    In this code you have subscribed for ResultsList_ItemDataBound and Pager_SelectedPageChanged but the actual void methods are not implemented. You have to implement them. You have to extract the page ID from the snippet of CustomDataSource ResultItem, then create a new ResultItem generic list and bind the Repeater control to the newly added list which should contain only the items you are looking for.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    08 Apr 2010
    Link to this post
    Hey Ivan,

    Thanks for the reply, this makes complete sense. Can you provide the code for ResultsList_ItemDataBound and Pager_SelectedPageChanged? I do not wish to change the original functionality of these methods, but have no reference as what code is placed inside them for the original code.

    Also, if this code is in a custom Lib within the /bin/ folder of the project, will the SearchResults control automatically pick up my override?

    Thanks
  6. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    09 Apr 2010
    Link to this post
    Hey Ivan,

    Any word on this? Is there somewhere I could reference the code for these methods?

    Thanks
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Apr 2010
    Link to this post
    Hi mcpdinkansas,

    Here is the code that you need to add.

    void ResultsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Footer)
          {
              if (!this.AllowPaging)
              {
                  e.Item.Controls.Clear ( );
                  return;
              }
          }
      }
     
      void Pager_SelectedPageChanged(object sender, EventArgs e)
      {
          this.CurrentPage = ((Pager)sender).SelectedPage;
          this.ChildControlsCreated = false;
      }

    SearchResults cannot pick up your override. You need to create a custom control that will be used instead of the built-in SearchResults control.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  8. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    09 Apr 2010
    Link to this post
    Thanks Ivan!!

    Your help is much appreciated. You are the support master.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Apr 2010
    Link to this post
    Hi mcpdinkansas,

    Always glad to help to the community!

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    12 Apr 2010
    Link to this post
    Hey Ivan,

    I am having trouble getting my custom control to attach to my new class to override the search result. Any help would be awesome.

    Custom Control ASCX: ~/Sitefinity/UserControls/SearchResultsControl.ascx + .cs (The code behind is a partial class tied to SearchResultsControlBase)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using Lib.ui.WebControls;
     
    public partial class Sitefinity_UserControls_CustomSearchResults_SearchResultsControl : SearchResultsControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }  
    }


    Inside my class Lib I have:

    SearchResultsControlBase.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    namespace Lib.ui.WebControls
    {
        public class SearchResultsControlBase : UserControl
        {
            string test = "";
        }
    }

    SearchResultsControl.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    namespace Lib.ui.WebControls
    {
        using Telerik.Framework.Web.Design;
        using Telerik.Cms.Web.UI;
        using Telerik.Search.WebControls;
        using Telerik.Search.Common;
        using System.IO;
        using System.Collections.Generic;
        using Telerik.Search.Engine;
        using Telerik.Utilities;
     
        public class SearchResultsControl : SearchResult
        {
            private Container container;
     
            protected override void CreateChildControls()
            {
                this.Controls.Clear();
     
                // Load the ASCX template assocated with this SearchResults
                string path = "~/Sitefinity/UserControls/CustomSearch/SearchResultsControl.ascx";
                ITemplate itemTemplate = this.Page.LoadTemplate(path);
     
                string CatalogName = "Full+Site+Page+Index";
     
                base.CreateChildControls();
     
                this.container = new Container(this);
     
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.DataBind();
     
                itemTemplate.InstantiateIn(container);
     
                if (!string.IsNullOrEmpty(Query))
                {
                    string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
                    string nPath = PathUtil.GetIndexPhysicalPath(CatalogName);
                    if (Directory.Exists(vPath) && Directory.Exists(nPath))
                    {
                        // check if query string is supplied to select the current page
                        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
                        {
                            this.CurrentPage = Convert.ToInt32(
                                HttpContext.Current.Request.QueryString[PageKey]);
                        }
     
                        string searchQuery = Query;
                        string message = string.Empty;
     
                        bool isValid = this.EscapeSpecialChars ? this.EscapeSpecialChars : ValidateQuery(ref searchQuery, out message);
     
                        if (isValid)
                        {
                            int totalItems = 0;
                            int totalItems2 = 0;
                            int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
     
                            try
                            {
     
                                IList<ResultItem> DataSource = SearchManager.Search(
                                       searchQuery,
                                       CatalogName,
                                       startIndex,
                                       this.PostsPerPage,
                                       this.WordsMode,
                                       this.EscapeSpecialChars,
                                       out totalItems2);
     
                                foreach (ResultItem customItem in DataSource)
                                {
                                    if (customItem.Url.Contains(""))
                                    {
                                        string test = "";
                                    }
                                }
     
                                totalItems = totalItems + totalItems2;
     
                                int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
     
                                if (numberOfPages == 0 && totalItems > 0)
                                    numberOfPages = 1;
     
                                string qeryTest = this.Query.Trim('\"');
                                ((SearchResultsControlBase)this.layoutCnt.ResultsStats).EnableViewState = false;
                                this.container.ResultsStats.Text = string.Format(this.container.ResultsStats.Text, totalItems, qeryTest);
     
                                if (this.AllowPaging)
                                {
                                    this.container.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);
                                    this.container.Pager1.PageCount = numberOfPages;
                                    this.container.Pager1.SelectedPage = this.CurrentPage;
                                }
                                this.container.ResultsList.DataSource = DataSource;
     
                            }
                            catch (Telerik.Lucene.Net.QueryParsers.ParseException ex)
                            {
                                this.container.ResultsStats.Text = "strange chars";
                                Log.Exception(ex);
                            }
                        }
                        else
                        {
                            this.container.ResultsStats.Text = message;
                        }
                    }
                    else
                    {
                        this.container.ResultsStats.Text = String.Format("InvalidIndex", this.IndexCatalogue);
                    }
                }
                else
                {
                    this.container.ResultsStats.Text = String.Empty;
                }
     
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.SkinID = this.SkinID;
     
                this.Controls.Add(this.container);
                this.container.ResultsList.DataBind();
     
            }
     
            void ResultsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    if (!this.AllowPaging)
                    {
                        e.Item.Controls.Clear();
                        return;
                    }
                }
            }
     
            void Pager_SelectedPageChanged(object sender, EventArgs e)
            {
                this.CurrentPage = ((Pager)sender).SelectedPage;
                this.ChildControlsCreated = false;
            }
        }
    }

    SearchResultsControlToolboxItem.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace Lib.ui.WebControls
    {
        public class SearchResultsControlToolboxItem : System.Drawing.Design.ToolboxItem
        {
            /// <summary>
            /// Initializes the toolbox item with the proper type
            /// </summary>
            /// <remarks>
            /// The section under which the control is initially placed is determined by the
            /// [assembly: AssemblyCompany("Telerik")] attribute.
            /// Of course, it can later be changed to something else throuh web.config
            /// </remarks>
            public SearchResultsControlToolboxItem()
                : base(typeof(SearchResultsControl))
            {
                base.DisplayName = "Page Group Search Results";
                base.Description = "Displays search results for individual Page Groups";
            }
        }
    }


    What am I missing to tie my custom control to my new class?

    Thanks


  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Apr 2010
    Link to this post
    Hi mcpdinkansas,

    You could register the SearchResultsControl in your web.config and drop the custom control directly on a page

    sample

    <add name="CustomSearchControl" section="Custom" type="Lib.ui.WebControls.SearchResultsControl, App_Code"/>

    If the class is located in App_Code you need to set the location as above, otherwise type the assembly name after the comma. Then the control will appear in "Custom" section of your toolbox when you open the page editor.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  12. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    12 Apr 2010
    Link to this post
    Thanks, I needed a second pair of eyes. I was registering the ASCX control in the web.config and not my class.

    Thanks again Ivan
  13. HRC
    HRC avatar
    44 posts
    Registered:
    16 Mar 2009
    04 May 2010
    Link to this post
    When I try to use this code and put the following changes in web.config file I get this error when attempting to add the control to a page.

    Exception Details: System.ArgumentException: The relative virtual path 'System.ArgumentException: The relative virtual path 'Lib.ui.WebControls.SearchResultsControl, App_Code' is not allowed here.

    How did you manage to get around this error?
  14. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    04 May 2010
    Link to this post
    Are you using an external Library or the App_Code folder?

    If external library, the web.config node needs to look like:

    <add name="CustomSearchControl" section="Custom" type="Lib.ui.WebControls.SearchResultsControl, LibNamespace"/>

    Of course change LibNamespace to your correct namespace of your Library.

    If using App_Code folder, the web.config node needs to look like:

    <add name="CustomSearchControl" section="Custom" type="ClassName, App_Code"/>
  15. Michael
    Michael avatar
    32 posts
    Registered:
    14 Jan 2010
    11 May 2010
    Link to this post
    Hello,

    Do you still need help with this? I've just noticed you post a made on a similar question I asked.

    (Sorry just noticed you found an answer that suits you)
  16. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    07 Jun 2010
    Link to this post
    Hi - Using the example above and the example on page http://www.sitefinity.com/devnet/forums/sitefinity-3-x/developing-with-sitefinity/multiple-search-result-controls.aspx
    I have tried to create a custom results control that will eventually filter resuts based on the users role when logged in.  I am have trouble however just getting the class to be read. I have registed it in the web confog and placed the control on the page. However it does not seem to be reading the class at all.  I am really missing something here.  I even relaced "title" with some text just to see if it is being read and output which it is not. I have placed my code below.  Thank you very much for any help you can give me. I have tried many things.
    Also, do I need string IndexName = "Resources+Publications";  since I will be using the control's index?
    using System;
    using System.IO;  
    using System.Collections.Generic; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.ComponentModel; 
    using System.Web.UI.HtmlControls; 
    using System.Globalization;
     using System.Collections.Generic;
      
    using Telerik.Search.Engine; 
    using Telerik.Search.Common; 
    using Telerik.Utilities; 
    using Telerik.Search.WebControls; 
    using Telerik.Cms.Web.UI; 
    using Telerik.Framework.Web; 
    using Telerik.Search.WebControls.Admin;
    using Telerik.Framework.Web.Design;
      
      
    namespace CustomSearchResults
    {
      
        //SearchResultsControl
        public class CustomSearchResult : SearchResult
        {
            private Container container;
      
            protected override void CreateChildControls()
            {
                this.Controls.Clear();
      
                // Load the ASCX template assocated with this SearchResults 
                string path = "~/Sitefinity/UserControls/Search/CustomSearchResult.ascx";
                ITemplate itemTemplate = this.Page.LoadTemplate(path);
      
                string IndexName = "Resources+Publications";
      
                //base.CreateChildControls();
      
                this.container = new Container(this);
                itemTemplate.InstantiateIn(container);
      
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.DataBind();
      
                  
      
                if (!string.IsNullOrEmpty(Query))
                {
                    string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
                    string nPath = PathUtil.GetIndexPhysicalPath(IndexName);
                    if (Directory.Exists(vPath) && Directory.Exists(nPath))
                    {
                        // check if query string is supplied to select the current page
                        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
                        {
                            this.CurrentPage = Convert.ToInt32(
                                HttpContext.Current.Request.QueryString[PageKey]);
                        }
      
                        string searchQuery = Query;
                        string message = string.Empty;
      
                        bool isValid = this.EscapeSpecialChars ? this.EscapeSpecialChars : ValidateQuery(ref searchQuery, out message);
      
                        if (isValid)
                        {
                            int totalItems;
      
                            int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
      
                            try
                            {
      
                                IList<ResultItem> DataSource = SearchManager.Search(
                                       searchQuery,
                                       IndexName,
                                       startIndex,
                                      this.PostsPerPage,
                                       this.WordsMode,
                                       this.EscapeSpecialChars,
                                       out totalItems);
      
                                foreach (ResultItem customItem in DataSource)
                                {
                                    //if (customItem.Url.Contains("resources"))
                                    //{
                                    //    string test = "testing";
                                    //}
                                    var contentInfo = new ResultItem();
                                    contentInfo.Title = "Hello"; // customItem.Title.ToString();
                                    contentInfo.Snippet = customItem.Snippet.ToString();
                                    contentInfo.Url = customItem.Url;
                                 
                                    // Add content item to List                
      
                                    DataSource.Add(contentInfo);
                                 }            
                                    // Return content index            
                                   // return DataSource; // contentItems.ToArray();
      
      
                                int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
      
                                if (numberOfPages == 0 && totalItems > 0)
                                    numberOfPages = 1;
      
                                string qeryTest = this.Query.Trim('\"');
                                ((System.Web.UI.Control)this.layoutCnt.ResultsStats).EnableViewState = false;
                                this.container.ResultsStats.Text = string.Format(this.container.ResultsStats.Text, totalItems, qeryTest);
      
                                if (this.AllowPaging)
                                {
                                    this.container.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);
                                    this.container.Pager1.PageCount = numberOfPages;
                                    this.container.Pager1.SelectedPage = this.CurrentPage;
                                }
                                  
                                this.container.ResultsList.DataSource = DataSource;
                                this.container.ResultsList.DataBind(); 
      
                            }
                            catch (Telerik.Lucene.Net.QueryParsers.ParseException ex)
                            {
                                this.container.ResultsStats.Text = "strange chars";
                                Log.Exception(ex);
                            }
                        }
                        else
                        {
                            this.container.ResultsStats.Text = message;
                        }
                    }
                    else
                    {
                        this.container.ResultsStats.Text = String.Format("InvalidIndex", this.IndexCatalogue);
                    }
                }
                else
                {
                    this.container.ResultsStats.Text = String.Empty;
                }
      
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.SkinID = this.SkinID;
      
                this.Controls.Add(this.container);
                this.container.ResultsList.DataBind();
      
            }
      
            void ResultsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    if (!this.AllowPaging)
                    {
                        e.Item.Controls.Clear();
                        return;
                    }
                }
            }
      
            void Pager_SelectedPageChanged(object sender, EventArgs e)
            {
                this.CurrentPage = ((Pager)sender).SelectedPage;
                this.ChildControlsCreated = false;
            }
        }
    }
  17. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Jun 2010
    Link to this post
    Hi Laura,

    You need to create a ToolBoxItem for your SearchResults control.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  18. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    07 Jun 2010
    Link to this post
    Thanks. I have now added the classes as in the link and the using [classname] on the template.
    This is being compiled into a .dll
    But I don't think it is still being read.  I have hardcoded some parameters and nothing is effected. Also, the template is not being loaded. I have to put the path in, in the control itself. I am really missing something here!
    Also, is it inside the For Each that I do a check for role and then output the pages based on their access?

    if (isValid)
    {
        int totalItems = 0;
        int totalItems2 = 0;
        int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
        try
        {
            IList<ResultItem> DataSource = SearchManager.Search(
                   "publication",
                   "ALL+Pages",
                   startIndex,
                   this.PostsPerPage,
                   this.WordsMode,
                   this.EscapeSpecialChars,
                   out totalItems2);
            foreach (ResultItem customItem in DataSource)
            {
                if (customItem.Url.Contains(""))
                {
                    string test = "";
                }
            }
            totalItems = totalItems + totalItems2;
            int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
            if (numberOfPages == 0 && totalItems > 0)
                numberOfPages = 1;
            string qeryTest = this.Query.Trim('\"');
            ((SearchResultsControlBase)this.container.ResultsStats).EnableViewState = false;
            this.container.ResultsStats.Text = string.Format(this.container.ResultsStats.Text, totalItems, qeryTest);
  19. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Jun 2010
    Link to this post
    Hello Laura,

    Attach  the code to VS debugger and step inside CreateChildControls to see whether there are any items in DataSource generic list.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  20. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    14 Jun 2010
    Link to this post

    I do have the same issue on the search function. I would like the search result display based on the users role when logged in. In the foreach below, how do I check if the person have permission to access the customItem and then display in the result page? Thanks, ~Pik

    . . .
    foreach (ResultItem customItem in DataSource)

            {
                ...
            }
    ...
  21. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    14 Jun 2010
    Link to this post
    Hi Pik Lee,

    You have to add the content id to the SearchResults snippet- Search to Provide Article ID . Then extract the id, find the content item or page by calling ContentManager or CmsManager. Afterwords check the user roles UserManager.GetCurrentUserRoles

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  22. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    14 Jun 2010
    Link to this post
    Thanks for the info. I tested the following code and received an error message. I am not sure how to get the ID value.
    Thanks, ~Pik

    string value = customItem.Snippet;
    string ID = value.Substring(0,36);

    this.layoutCnt.ResultsStats.Text = ID;

  23. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    15 Jun 2010
    Link to this post
    Not so sure what is wrong with the following code.  It does display ID if I changed the substring from (0, 36)  to (0,20).  Any idea why? 

    string ID = value.Substring(0,36);

    Also, how do i display the data in the search results in the following code:
    . . .
    foreach (ResultItem customItem in dataSource) {
      . . .
    }
    . . .

    Thanks,~Pik
  24. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Jun 2010
    Link to this post
    Hi Pik Lee,

    For the first issue you could take a look at String Methods. For the second issue -  you need to create a new generic list of type ResultItem and bind it to the repeater control this.container.ResultsList.DataSource

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  25. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    16 Jun 2010
    Link to this post
    Thanks for the reply. Is that a way to search only the title of the page not the content?

    Thanks, Pik
  26. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    17 Jun 2010
    Link to this post
    Hi Pik Lee,

    You can create a page index, then go to ~/App_Data/Search/[YourPageIndexName]/fieldsInfoProvider.xml and make the search index index only page's title and none of the tags containing body (check your master pages for the id's of those elements):
    <?xml version="1.0" encoding="utf-8"?>
    <fields>
        <!--Give more imporance to page title by increasing weight attribute-->
      <field name="title" weight="1" indexAttribute="" filterTag="title" filterAttributes="" />
      <field name="keywords" weight="1" indexAttribute="content" filterTag="meta" filterAttributes="name:keywords;" />
      <field name="description" weight="1" indexAttribute="content" filterTag="meta" filterAttributes="name:description;" />
      <field name="script" weight="-1" indexAttribute="" filterTag="script" filterAttributes="" />
      <field name="style" weight="-1" indexAttribute="" filterTag="style" filterAttributes="" />
      <!--Do not index content contained within div tags with id content or body by setting weight to -1-->
      <field name="div" weight="-1" indexAttribute="" filterTag="div" filterAttributes="id:content" />
      <field name="div" weight="-1" indexAttribute="" filterTag="div" filterAttributes="id:body" />
    </fields>


    Regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  27. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    18 Jun 2010
    Link to this post
    Quick question - if I have in my App_Code folder I have sub folders:
    App_Code
            CS_Code
            CustomSearchResults (and inside the class customsearchresult.cs)        
            VB_Code

    In the web.config I did put

    <

     

    codeSubDirectories>

     

    <

     

    add directoryName="VB_Code"/>

     

    <

     

    add directoryName="CS_Code"/>

     

    <

     

    add directoryName="CustomSearchResults"/>

     

    </

     

    codeSubDirectories>

     


    How do I reference the class customsearchresult.cs in the webconfig? I can not get this to load correctly! It keeps saying error loading control in the edit page mode in the select a contol panel.

    I have heard that if you use folders you then don't refernce the App_Code folder as such (both of these example don't work though:
    <

     

    add name="Custom Search Results" section="Custom" type="CustomSearchResult"/>
    OR
    <add name="Custom Search Results" section="Custom" type="CustomSearchResults.CustomSearchResult"/>

    Thank you!

     

  28. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    18 Jun 2010
    Link to this post
    I am thinking that it has something to do with my toolbox code?
    Is my namespace to be CustomSearchResults which is the folder inside App_Code and also houses thes classes CustomSearchResult.cs, SearchResultsControlBase.cs, and SearchResultsControlToolboxItem.cs??
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Design;
    using Telerik.Web;
      
      
        /// <summary>
        /// This will determine what will be shown in the toolbox
        /// </summary>
    namespace CustomSearchResults
    {
        public class SearchResultsControlToolboxItem : System.Drawing.Design.ToolboxItem
        {
            /// <summary>
            /// Initializes the toolbox item with the proper type
            /// </summary>
            /// <remarks>
            /// The section under which the control is initially placed is determined by the
            /// [assembly: AssemblyCompany("Telerik")] attribute.
            /// Of course, it can later be changed to something else throuh web.config
            /// </remarks>
            public SearchResultsControlToolboxItem()
                : base(typeof(CustomSearchResult))
            {
                base.DisplayName = "Custom Searech Results";
                base.Description = "Displays custom search results.";
            }
      
      
        }
      
    }     
  29. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Jun 2010
    Link to this post
    Hello Laura,

    You should add App_Code the the type of the web.config declaration of your custom class. Note that you should register the control, not its toolbox item.


    sample

    public class ResultToolboxItemCustom : System.Drawing.Design.ToolboxItem
    {
        public ResultToolboxItemCustom()
            : base(typeof(SearchResultsCustom))
        {
            base.DisplayName = "CustomSearch";
            base.Description = "Custom Search Control";
        }
    }
     
    [System.ComponentModel.ToolboxItem(typeof(ResultToolboxItemCustom))]
    public class SearchResultsCustom : SearchResult
    {
        public SearchResultsCustom()
        {
        }
    }

    you can register the toolbox in the same class and namespace or in a separate class file.


    <add name="SearchResultsCustom" section="Custom" type="SearchResultsCustom, App_Code" />


    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  30. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    21 Jun 2010
    Link to this post
    I am so sorry - all these examples are getting confusing. This is the last task to be completed so the site can go live. Can you or someone please please please post a new clear example?  I just want to be able to access the output of the search results control. 

    Example:
    New ASCX control name:  ~/Custom/Controls/NewSearchResultsControl.ascx
    New Class:  ~/App_Code/CS_Code/CustomSearchResultsClass.cs
    I have registered the CS_Code folder in App_Code in the webconfg.  (<add directoryName="CS_Code"/>)

    In the code behind of the ~/Custom/Controls/NewSearchResultsControl.ascx I only put (?)
    public partial class Custom_Controls_NewSearchResultsControl : SearchResultsControlBase

      protected void Page_Load(object sender, EventArgs e)
        {}
    }

    In the SearchResultsControlBase I have: (do I need this?)
    public class SearchResultsControlBase : UserControl
       {
           string test = "";
       }

    In the CustomSearchResultsClass.cs I put ?

    Thank you very much in advance!!!!
Register for webinar
44 posts, 1 answered
1 2