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

Forums / Developing with Sitefinity / How to add FilterExpression to SimpleControl

How to add FilterExpression to SimpleControl

5 posts, 1 answered
  1. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    04 Apr 2010
    Link to this post
    I am having trouble adding a FilterExpression property to a simple control. I would like the admin user to enter their FilterExpression from the control designer. So in my SimpleControl, I implemented the interface IFilterableContentControl and it added the FilterExpressing string property.

    But how do I use the FilterExpression string to build the content filter? This is what I have so far:

    this.DefaultFilter = new ContentFilterBuilder(this);
    this.DefaultFilter.AddFilter(new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual, JoinType.And));
    this.DefaultFilter.AddFilter(new ContentFilterStatement(???this.FilterExpression???));
     
    IList newsList = this.Manager.Content.GetContent(0, 0, "Publication_Date DESC", String.Empty, ContentStatus.Published, String.Empty, null, this.DefaultFilter.ParseMetaFieldsFilter());

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Apr 2010
    Link to this post
    Hello bemara57,

    Below is a sample code of IFilterable control. You could take a look at ControlDataSource() to see how to add statements to the filter. You should call builder.AddFilter(new ContentFilterStatement(...).
    You do not have to use MetaSearchInfo. ContentFilterStatement has five overloads that you can use

    public class SimpleControlCustom : SimpleControl, IFilterableContentControl
    {
        public SimpleControlCustom()
        {
     
        }
     
        public override string LayoutTemplatePath
        {
            get
            {
                return "~/CustomControls/SimpleControlCustomTemplate.ascx";
            }
     
        }
     
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }
     
        protected override void InitializeControls(System.Web.UI.Control controlContainer)
        {
            base.InitializeControls(controlContainer);
            if (RepeaterControl != null)
            {
                RepeaterControl.DataSource = ControlDataSource();
                RepeaterControl.ItemDataBound += new RepeaterItemEventHandler(RepeaterControl_ItemDataBound);
                RepeaterControl.DataBind();
            }
        }
     
        void RepeaterControl_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var dItem = e.Item.DataItem as IContent;
                var showTitle = e.Item.FindControl("Title") as Label;
                showTitle.Text = (string)dItem.GetMetaData("Name");
            }
        }
     
     
        public virtual IList ControlDataSource()
        {
            var Manager = new ContentManager(ProviderName);
            ContentFilterBuilder builder = new ContentFilterBuilder(this);
      
            if (FilterBySpecifiedCategory)
            {
                // here you can apply the filter so that only certain items will be returned.
                return Manager.GetContent(0, 0, String.Empty,
                                              builder.ParseTagFilter(), ContentStatus.Published, null,
                                              builder.ParseParentsFilter(), builder.ParseMetaFieldsFilter());
               // or apply some rule directly    
              //  builder.AddFilter(new ContentFilterStatement("Category", "SomeCategoryName", ContentFilter.Condition.In, ContentFilter.JoinType.And));
                 
            }
            else
            {
                IList allItems = Manager.GetContent();
                return allItems;
            }
            
        }
     
        public virtual Repeater RepeaterControl
        {
            get
            {
                return this.Container.GetControl<Repeater>("Repeater1", true);
            }
        }
     
     
        public bool FilterBySpecifiedCategory
        {
            get;
            set;
        }
     
        #region IFilterableContentControl Members
     
        public string FilterExpression
        {
            get
            {
                object obj = ViewState["FilterExpression"];
                if (obj != null)
                    return (string)obj;
                return string.Empty;
            }
            set { ViewState["FilterExpression"] = value; }
        }
     
        public string ProviderName
        {
            get
            {
                object obj = ViewState["ProviderName"];
                if (obj != null)
                    return (string)obj;
                return string.Empty;
            }
            set { ViewState["ProviderName"] = value; }
        }
     
        #endregion
    }


    <asp:Repeater runat="server" ID="Repeater1">
    <HeaderTemplate></HeaderTemplate>
       <ItemTemplate>
           <asp:Label runat="server" ID="Title"></asp:Label>
       </ItemTemplate>
    <FooterTemplate></FooterTemplate>
    </asp:Repeater>

    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.
    Answered
  3. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    05 Apr 2010
    Link to this post
    The ContentFilterStatement does have 5 overloads, but none accept a complete FilterExpression string. Say my FilterExpression is loaded with "Title LIKE 'Demo%'", am I supposed to parse it into ContentFilterStatement? I can't do something like this:

    DefaultFilter.AddFilter(new ContentFilterStatement(this.FilterExpression))
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Apr 2010
    Link to this post
    Hello bemara57,

    There are ContentFilter.Condition and ContentFilter.JoinType.


    • ContentFilter.Condition.Like:
    • ContentFilter.Condition.NotEqual:
    • ContentFilter.Condition.GreaterThan:
    • ContentFilter.Condition.LessThan:
    • ContentFilter.Condition.GreaterOrEqual:
    • ContentFilter.Condition.LessOrEqual:
    • ContentFilter.Condition.NotNull:
    Pass left and right operands and then the proper condition.
    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. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    05 Apr 2010
    Link to this post
    The FilterExpression's relation to the ParseInternal method wasn't so obvious at first. This is what I ended up with:

    public class SomeClass : SimpleControl, IFilterableContentControl
    {
        #region IFilterableContentControl Members
         
        public string FilterExpression
        {
            get;
            set;
        }
     
        #endregion
     
        protected virtual ContentFilterBuilder DefaultFilter
        {
            get
            {
                if (_defaultFilter == null)
                {
                    _defaultFilter = new ContentFilterBuilder(this);
                    if (!String.IsNullOrEmpty(this.FilterExpression))
                    {
                        foreach (IContentFilterStatement item in _defaultFilter.ParseInternal(true))
                        {
                            _defaultFilter.AddFilter(item);
                        }
                    }
                }
                return _defaultFilter;
            }
        }
    }

    Much appreciated on the response!
Register for webinar
5 posts, 1 answered