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

Forums / General Discussions / How to create QueryItems?

How to create QueryItems?

8 posts, 1 answered
  1. Hahn
    Hahn avatar
    11 posts
    Registered:
    22 Mar 2011
    04 Apr 2011
    Link to this post
    I currently have a requirement where I need to dynamically create several NewsView controls that each take in a category argument, and only display news items from that category.  

    I've got it working fine, but it's really messy.  I had to set the NewsView's AdditionalFilter property with my own QueryData and QueryItems... the contents of which I don't fully understand but have just adapted from the output of NewsViews which I've created via the built in designer.  

    Is there a proper way to create QueryData via some utility class?  I see a QueryBuilder namespace but am not yet sure what it's for or how to use it
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    06 Apr 2011
    Link to this post
    Hello Hahn,

    Actually you can use FilterExpression of the ContentView based control

    sample

    NewsView v = new NewsView();
     
    var filter = "Category.Contains(\"{A9F2C13A-9472-4BF6-AB3D-19B2EF05B043}\") AND Visible = true AND Status = Live AND PublicationDate <= DateTime.UtcNow AND (ExpirationDate = NULL OR ExpirationDate > DateTime.UtcNow)";
     
    v.MasterViewDefinition.FilterExpression = filter;
     
    Controls.Add(v);


    Regards,
    Ivan Dimitrov
    the Telerik team

    Answered
  3. Hahn
    Hahn avatar
    11 posts
    Registered:
    22 Mar 2011
    06 Apr 2011
    Link to this post
    works great.   thank you for the response and for making the filter complete, as I would not have thought to include live status and expiration date into the filter.  
  4. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    25 May 2011
    Link to this post
    Is there syntax for filtering by custom fields?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 May 2011
    Link to this post
    Hi Basem,

    Here is a sample that shows filtering by custom fields

    Visible = true AND Status = Live 
    AND Tags.Contains((95BFB3D9-10EE-491f-BB95-5B43423404D5))
    AND CustomField.Contains((83F51312-C941-43a1-BC5E-605A2BC288D5))


    There were errors in the previous versions that should be fixed in the latest build.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Peter Kinmond
    Peter Kinmond avatar
    11 posts
    Registered:
    22 Dec 2010
    26 May 2011
    Link to this post
    Hi Ivan,

    I have a custom widget and I want to dynamically add a NewsView with a filter by using the code snippet you showed before:

    NewsView v = new NewsView();
      
    var filter = "Category.Contains(\"{A9F2C13A-9472-4BF6-AB3D-19B2EF05B043}\") AND Visible = true AND Status = Live AND PublicationDate <= DateTime.UtcNow AND (ExpirationDate = NULL OR ExpirationDate > DateTime.UtcNow)";
      
    v.MasterViewDefinition.FilterExpression = filter;
      
    Controls.Add(v);

    I tried adding it into the CreateChildControls() and the Page_Load() methods and in both cases the NewsView appears correctly but the filter doesn't work (all of the news items appear, rather than just the ones with the given category). I'm sure the filter text is correct because I was able get the it working by adding it to the FilterExpression in the Advanced settings of the NewsView widget. Can you tell where I should put the above code to have the filter work correctly? Are there any other steps I need to take to get the filter working for a dynamically created NewsView?

    Thanks,
    Peter
  7. Peter Kinmond
    Peter Kinmond avatar
    11 posts
    Registered:
    22 Dec 2010
    26 May 2011
    Link to this post
    Hi Hahn,

    I'm also trying to achieve this. Would it be possible for you to post some of your code here that has this working? Are you using a custom widget?

    Thanks,
    Peter
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    26 May 2011
    Link to this post
    Hello Peter,

    I tried the same at my end and the source was correctly filtered. I  used Page_Load of a user control.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
8 posts, 1 answered