Filter NewsItems by date

Topics: Developer Forum
Apr 10, 2009 at 5:38 PM
I have modified the NewsList view and would like to be able to filter the items based on year och month, let's say all items published in april 2009. Month and year will be included in the querystring. How do I modify the filter event to include a date filter?

void idsNews_Filtering(object sender, N2.Collections.ItemListEventArgs e)
        {
            Collections.TypeFilter.Filter(e.Items, typeof(N2.Templates.Items.News));
        }
Coordinator
Apr 16, 2009 at 9:09 PM
You can create your own filter by inheriting from N2.Collections.ItemFilter and implementing the Match method. Then you can use it like this:

void idsNews_Filtering(object sender, N2.Collections.ItemListEventArgs e)
        {
            ...
            new DateFileter(GetStartDate(), GetStopDate()).Filter(e.Items)
        }
Apr 16, 2009 at 9:17 PM
Thank you.

Could you please give some hints on how to "by inheriting from N2.Collections.ItemFilter and implementing the Match method"?

// Johan
Coordinator
Apr 16, 2009 at 11:01 PM
How about an example?

Look at the published filter http://code.google.com/p/n2cms/source/browse/trunk/src/N2/Collections/PublishedFilter.cs

It compares the current date against the item's published and expire dates. There are some other classes inheriting from item filter in that namespace if you look around.