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

Forums / Developing with Sitefinity / Change default FilterExpression for View control in News derived extension module

Change default FilterExpression for View control in News derived extension module

7 posts, 1 answered
  1. Dave
    Dave avatar
    20 posts
    Registered:
    10 Aug 2010
    28 Dec 2010
    Link to this post
    I created a new module derived from News items using Gabe Sumner's extremely useful blog post How to create new content types using Generic Content Modules – Webinar. For various reasons, I want to change the default filter that is set in the FilterExpression property when a new instance of the App_Code\xxView.cs control is dropped onto a page.

    In searching the forums, I found this November 2008 post by Pepi, which provides some overrides to change the FilterExpression, but it appears to hard code the filter expression, so that no matter what I change it to in the control's advanced properties page in the CMS UI, it always uses the filter generated by the code.

    For the record, my version of the overrides from Pepi's post are provided here:

    protected override IList CreateDataSource()
    {
        ContentFilterBuilder filterBuilder = this.GetFilterBuilder();
        if (filterBuilder.IsFilterValid)
            return this.Manager.GetContent(base.GetContentStartIndex(),
                                 base.GetPageSize(),
                                 base.SortExpression,
                                 filterBuilder.ParseTagFilter(),
                                 ContentStatus.Published,
                                 null,
                                 filterBuilder.ParseParentsFilter(),
                                 filterBuilder.ParseMetaFieldsFilter());
     
        return base.CreateDataSource();
    }
     
    protected override ContentFilterBuilder GetFilterBuilder()
    {
        ContentFilterBuilder filterBuilder = base.GetFilterBuilder();
        filterBuilder.ClearFilter();
     
        MetaSearchInfo filter = new MetaSearchInfo(MetaValueTypes.DateTime,
                                      "Publication_Date",
                                      DateTime.Now,
                                      SearchCondition.LessOrEqual);
     
        filterBuilder.AddFilter(filter);
     
        return filterBuilder;
    }

    Note that the first function is unchanged, but the second one first clears the existing filter then adds the criteria I want, rather than just adding to the existing criteria like Pepi's example does.

    So the question is, how do I just change the default FilterExpression that is applied to new controls when they are dropped on to a page?

    Thanks,
    Dave Parker
    Coherent Interactive
    Seattle, WA

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    28 Dec 2010
    Link to this post
    Hi Dave,

    1. The control has a public property -FilterExpression that you can modify
    2. You can clear the filter inside CreateChildControls, then create a new instance of ContentFilterBuilder and apply your custom rules. This should happen before calling the base.

    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
  3. Dave
    Dave avatar
    20 posts
    Registered:
    10 Aug 2010
    28 Dec 2010
    Link to this post
    Thanks for the prompt response, Ivan.

    Based on your reply, I tried three different approaches. In all cases I removed the overrides of GetDataSource and GetFilterBuilder and overrode CreateChildControls. Then, inside of CreateChildControls:

    - For the first attempt, I tried using the ContentFilterBuilder to clear the filter and then create a new new one using the same MetaSearchInfo technique used in Pepi's post that I link above.  I then tried extracting the string filter expression from it and setting the FilterExpression property, but I could not figure out how to properly extract the string filter expression from the ContentFilterBuilder object.

    - For my second attempt, I called GetFilterBuilder() and used it to clear the filter.  I then just set the .FilterExpression property to "Publication_Date <= \"#now\"".  This filtered properly (as best I could tell), but when I dropped a new control instance onto a page, and viewed the Filter field of the Advanced properties, it still showed the original default filter (with Expiration_Date), and no amount of changing that field would affect which items were shown.

    - On my third attempt, I used the technique you provided in this post, but it had the exact same effect as my second attempt.

    So either I misinterpreted your response (which I freely admit is possible), or perhaps you misinterpreted my request.  In short, I still want people to be able to modify the FilterExpression using the Advanced tab of the control's properties when they place it onto a page, but I want to change the default filter expression that is applied when they first place the control on a page.  Any changes to the FilterExpression field that is done by CMS users after the control is dropped should be applied and persisted with the control's instance on the page.

    Thanks again,
    Dave Parker
    Coherent Interactive
    Seattle, WA

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    29 Dec 2010
    Link to this post
    Hi Dave,

    There is no event that is thrown when you drop a control on a page. You can create a custom property under Filters section and hide the default FilterExpression property. Then check if there is value set for your custom property and apply the this value to the base property,otherwise return string.Empty

    sample

    [Category("Filters")]
    public string FilterExpressionCustom
    {
        get
        {
                return this.CustomFilterExpression;
        }
        set
        {
            this.CustomFilterExpression = value;
        }
    }
     
    private string CustomFilterExpression;
     
    [Browsable(false)]
    public override string FilterExpression
    {
        get
        {
            if (FilterExpressionCustom != null && FilterExpressionCustom != String.Empty)
                return this.FilterExpressionCustom;
            return String.Empty;
        }
        set
        {
            base.FilterExpression = value;
        }
    }


    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
  5. Dave
    Dave avatar
    20 posts
    Registered:
    10 Aug 2010
    29 Dec 2010
    Link to this post
    Thanks, Ivan.  I understand what you are saying here, but for clarification, shouldn't the code return base.FilterExpression where there is no custom one?  Here is what I would think the correct code is (including some general refactoring):

    [Category("Filters")]
    public string CustomFilterExpression { get; set; }
      
    [Browsable(false)]
    public override string FilterExpression
    {
        get
        {
            if (!String.IsNullOrEmpty(this.CustomFilterExpression))
            {
                return this.CustomFilterExpression;
            }
            return base.FilterExpression;
        }
        set { base.FilterExpression = value; }
    }

    Thanks,
    Dave Parker
    Coherent Interactive
    Seattle, WA

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    29 Dec 2010
    Link to this post
    Hi Dave,

    If you return the base this will load the default filter including Publication_Date and Expiration_Date metafields.

    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
  7. Dave
    Dave avatar
    20 posts
    Registered:
    10 Aug 2010
    29 Dec 2010
    Link to this post
    Thanks again, Ivan.  For anyone looking for the solution to this problem here is my final code:

    public class ActionsView : Telerik.News.WebControls.NewsView
    {
        public ActionsView()
        {
            this.CustomFilterExpression = "Publication_Date <= \"#now\"";
        }

        //
        // [...overrides for ProviderName, ItemListTemplatePath, and SingleItemTemplatePath...]
        //

        [Category("Filters")]
        [Description("Defines the filter applied to this instance of the control.")]
        public string CustomFilterExpression { getset; }

        [Browsable(false)]
        public override string FilterExpression
        {
            get
            {
                if (!String.IsNullOrEmpty(this.CustomFilterExpression))
                {
                    return this.CustomFilterExpression;
                }
                return String.Empty;
            }
            set { base.FilterExpression = value; }
        }
    }


    Dave Parker
    Coherent Interactive
    Seattle, WA
Register for webinar
7 posts, 1 answered