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

Forums / Developing with Sitefinity / How to User Data FilterExpression for NewsList?

How to User Data FilterExpression for NewsList?

18 posts, 0 answered
  1. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    28 Jun 2007
    Link to this post
    Hi,

    We add custom fields in News module, now we want to filter items with those fields dynamically.

    How to define Filter Parameters and use FilterExpression in the News module?

    Thanks

    JW-AXYS
  2. Bob
    Bob avatar
    330 posts
    Registered:
    30 Dec 2016
    29 Jun 2007
    Link to this post
    Hello Charles,

    Sorry we have omitted to document this.

    Here is an example of filter:
    Author = ‘Me’; Title ~ ‘Word’; Priority > 1

    ~ is used for LIKE operator (% is not supported)
    ; stands for AND operator (OR is not supported)

    The rest of the operators are the same as in SQL.

    We will probably change this in the future to support SQL syntax.

    Kind regards,
    Bob
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    03 Jul 2007
    Link to this post

    Hi, Bob

    How to define Filter Parameters and use FilterExpression in the News module?

    In you example,

    Author = ‘Me’; Title ~ ‘Word’; Priority > 1

    How to assign a value dynamically in the expression Author=’<Parameter>

    How to access the parent datasource object for NewsList.ascx?

  4. Bob
    Bob avatar
    330 posts
    Registered:
    30 Dec 2016
    03 Jul 2007
    Link to this post
    Hello Charles,

    Unfortunately, there is no way to access datasource object for NewsList. This certainly is a shortcoming for the control and will provide this ability for the next release.

    If you want to set FilterExpression property programmatically, you need to either inherit the control and override the property or wrap NewsList in a User Control and set the property on some event such as Page_Load. Then you have to add your derived control to the Toolbox. Please see User Manual > Working with controls for reference on how to add controls to the Toolbox.

    Additionally, if you want to implement your own control, you can use ContentManager to retrieve News articles:
    ContentManager manager = new ContentManager("News");  
    manager.FilterExpression = "Author='John'";  
    IList list = manager.GetContent();  
     

    The list will contain IContent objects.

    Regards,
    Bob
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    12 Jul 2007
    Link to this post

    Hi, Bob

    I modify the NewList.ascx control as following:

    -----------------------------------------------------------------------

    <
    script runat="server" language="C#">

    enum Locations

    {

    Office=1,

    Region=2

    };

    private int _location = (int)Locations.Office;

    protected int Location

    {

    get { return _location; }

    set { _location = value; }

    }

    bool ShowLocation(string office, string region)

    {

    string p ;

    if (_location == (int)Locations.Office)

    {

    p = Profile.Office;

    return (office == p) ? true : false;

    }

    else

    {

    p = Profile.Region;

    return (region == p) ? true : false;

    }

    return true;

    }

    </script>

    <h2 class="notop">Hello,<%=Profile.FirstName%>, News From <%=Profile.Region +" "+Profile.Office%></h2>

    <asp:Repeater ID="newsRepeater" runat="server">

    <ItemTemplate>

    <asp:Panel ID="DisplayOrNot" Visible='<%#ShowLocation((string)DataBinder.Eval(Container.DataItem, "Office"),(string)DataBinder.Eval(Container.DataItem, "Region"))%>' runat="server">

    <p class="newsdate"><%#DataBinder.Eval(Container.DataItem, "Publication_Date", "{0:dd MMMM yyyy}")%></p>

    <h3 class="notop"><%#DataBinder.Eval(Container.DataItem, "Title")%></h3>

    <p class="notop">

    <asp:Image ID="Thumbnail" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "Thumbnail")%>' runat="server" style="float:left;margin:0 5px 3px 0" />

    <asp:Literal ID="Summary" Text='<%#DataBinder.Eval(Container.DataItem, "Summary")%>' />

    </p>

    <p class="notop" style="clear:left;"><asp:HyperLink ID="newsLink" runat="server" CssClass="astrong">more <em>&raquo;</em></asp:HyperLink></p>

    </asp:Panel>

    </ItemTemplate>

    </asp:Repeater>

    -------------------------------------------------

    My question is how to enable to edit the property "Location" on the Control Properties Grid after we add this control on a page? So we can reuse the control.

    Thanks

  6. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    13 Jul 2007
    Link to this post
    Hello Charles,

    All you need to do is set the property as public instead of protected. It should be visible when you click on Edit button on the control. Additionally you can set CategoryAttribute attribute if you want the property to appear in certain group otherwise it will appear in Misc group.

    Kind regards,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    13 Jul 2007
    Link to this post
    Thanks, Vassil

    I modify the property as

    [Telerik.News.WebControls.Admin.CategoryAttribute("Data")]

    public int Location

    {

    get { return _location; }

    set { _location = value; }

    }

    It still does not work.

    Also how to put my enum values in the drop-down list on the Control Editor?

    In this way I will have a editable "Location" property in the "Data" category, with a drop-down control who's items are "Office" and "Region"

    Thanks. 

  8. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    16 Jul 2007
    Link to this post
    Hi Charles,

    Regarding the category of your control, you can include the System.ComponentModel namespace and apply an attribute like this:

    using System.ComponentModel;    
    ...    
    [Category("SampleCategory")]    
    public int Location    
    {    
    ...    
    }    
     

    In order to be able to choose values from a dropdown in the property editor, declare your property as the enum type instead of int.

    All the best,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  9. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    16 Jul 2007
    Link to this post
    Hi,

    I modify the code as

    public enum Locations

    {

    Office=1,

    Region=2

    };

    private Locations _location = Locations.Office;

    [System.ComponentModel.Category("Data")]

    public Locations Location

    {

    get { return _location; }

    set { _location = value; }

    }


    But it doesn't work.

    Any problem?

    Thanks
  10. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    17 Jul 2007
    Link to this post
    Hello Charles,

    If you create a new user control named PropertyTest with only the code below in it, you should see the property enabled for editing under the Data category for the control properties window.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PropertyTest.ascx.cs" Inherits="UserControls_PropertyTest" %> 
     
    <script runat="server" language="C#">  
        public enum Locations  
        {  
            Office = 1,  
            Region = 2 
        };  
     
        private Locations _location = Locations.Office;  
     
        [System.ComponentModel.Category("Data")]  
        public Locations Location  
        {  
            get { return _location; }  
            set { _location = value; }  
        }      
    </script> 

    This is why it is strange that your control is behaving somewhat unexpectedly. Do you get any exceptions when you add the control to a page?On clicking Edit, does the window title show ExceptionControl instead of the name of your control type? If so, this means that there is some problem with the control.

    Greetings,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    17 Jul 2007
    Link to this post
    Hi,

    I didn't get an exception after click Edit, but without the property.
  12. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    17 Jul 2007
    Link to this post
    Hi Charles,

    Would you send us your control so we could take a closer look at the situation? It seems that we are missing something. Thanks for your cooperation in advance.

    Best wishes,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  13. Charles
    Charles avatar
    14 posts
    Registered:
    25 May 2007
    17 Jul 2007
    Link to this post
    Hi,

    Is it because I modify the NewList control directly?

  14. Jim Aderhold
    Jim Aderhold avatar
    48 posts
    Registered:
    29 Feb 2004
    18 Jul 2007
    Link to this post
    I have created a field with a data type of boolean. I then adjusted the insert and edit control by adding a checkbox for this new field. The checkbox works just fine and when I output the value it is matches with the True or False depending on whether the checkbox was checked or not. The problem I am having is that I can not seem to filter based off of this new field type. I have tried specifying the filter as field='True', field='true', field=True, and field=true. None of these will filter out only those items with the field set to true.
  15. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    19 Jul 2007
    Link to this post
    Hello Charles,

    First I want to apologize for the misunderstanding on my part. Rolling back to your old posts I saw that you want to modify the NewsList.ascx file by adding some C# code to it which is actually impossible. The .ascx file is used for a template and the <script runat="server"> tag does not extend the control's code which would use it. The <script runat="server"> tag is used in user controls.

    In case you would like to extend the NewsList control functionality, you should either create a new custom control which inherits from the NewsList control or create a user control encapsulating the NewsList and providing functionality which manages the control behavior.

    In case you are creating custom control, please check this forum thread: Changing Hyperlink Class in News Content
    The implementation should be similar.


    Jim,

    I tried to reproduce the problem to no avail. Everything seems to be working fine on my machine. Have you ensured that the field name is the same as the one you have added through the web.config. In my case, when I add Show field the filter expression is this: Show = 'True' . To be absolutely sure, you can check in the sf_CmsContentMetaData if the field name as well as its value are the same as the ones you specify in the filter expression.

    Kind regards,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  16. Jim Aderhold
    Jim Aderhold avatar
    48 posts
    Registered:
    29 Feb 2004
    19 Jul 2007
    Link to this post
    The field name that I am using for the filter is the same that is in the config and in the DB, even the case is the same. Like I mentioned in my previous message, I can output the value through the DataItem, in my NewsList.ascx file, and the value is outputed correctly. Everything works fine with the additional field as far as the admin section and the user end. The pnly problem I am having is the filtering. Maybe I will try it again using a different name. The site is running on SP1 so maybe I will try to update it to SP2. Not sure if it will make a difference or not.
  17. Vassil Daskalov
    Vassil Daskalov avatar
    261 posts
    Registered:
    18 May 2013
    20 Jul 2007
    Link to this post
    Hello Jim Aderhold,

    It is strange that you cannot filter the newly created fields. As I cannot see any reason why this would happen and in case you are still getting this behavior, it would be most appropriate to send us your project and database so we could take a closer look at the situation.

    Best wishes,
    Vassil Daskalov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  18. Jim Aderhold
    Jim Aderhold avatar
    48 posts
    Registered:
    29 Feb 2004
    20 Jul 2007
    Link to this post
    Vassil,
        I couldn't get the boolean data type to filter so I decided to try a different approach by changing the data type to ShortText and then creating a DropDownList to achieve the same goal. I was able to filter using this approach. I will create a support ticket and send you the project and DB.
Register for webinar
18 posts, 0 answered