Subscribe to our blog!


Filter Sitefinity content-view based controls by multiple months and years

  • custom archive control
By on

Read the built query string and filter blog posts list

The following blog post targets all controls in Sitefinity that are based on content view as News, Events, Blog posts etc. Let's take blog posts as example.

Sitefinity provides an amazingly quick and easy-to-use way to create blog posts via the Blogs content module. Naturally once you have more blog posts, you would want to display archive so that users could find older posts. Out-of-the-box there is an Archive widget that you could use to filter blog posts. You can use it to pick up the blog posts that are published on a specific day, month and year or just year.

In some cases you may want to display blog posts filtered by multiple dates of publication. Read below to understand how.

TIP: You can find the full code in GitHub here

Solution plan or preparing the weapons

You must customize the Archive control so that it allows selection of multiple publication dates. One possible scenario is to customize the template of the control and add checkbox list. Then, you must be able to check several checkboxes and apply the filter to the blog posts control. 
How the Archive control s filtering the Blog posts view by default? - by appending the filter to the url (depending on the UrlEvaluationMode property value either QueryString or QueryPath).

For example:

  • query string:


  • query path


You must write your own query string builder to the url depending on the checked checkboxes in the archive control. The usage of query strings allows the url to be shared. Desired result:


Warning: The bigger amount of checkboxes you check, the longer the url becomes with the query string. What is the limit?: 
Microsoft Internet Explorer (Browser) Microsoft states that the maximum length of a URL in Internet Explorer is 2,083 characters, with no more than 2,048 characters in the path portion of the URL. In my tests, attempts to use URLs longer than this produced a clear error message in Internet Explorer. 
Firefox (Browser) After 65,536 characters, the location bar no longer displays the URL in Windows Firefox 1.5.x. However, longer URLs will work. I stopped testing after 100,000 characters. 
Safari (Browser) At least 80,000 characters will work. I stopped testing after 80,000 characters. 
Opera (Browser) At least 190,000 characters will work. I stopped testing after 190,000 characters. Opera 9 for Windows continued to display a fully editable, copyable and pasteable URL in the location bar even at 190,000 characters.

Separate and conquer:

Now that you have a solution plan, it's time for action. 
Brace yourselves - code is coming!

Customize Archive widget template

TIP: The default template of the Archive control can be found in Sitefinity's github repository SitefinityResources.

Download the source code on top to see the customized template.

Implement custom url builder

Once you try to append more query string parameters to the url of the blog posts, you will receive an exception as Sitefinity does not allow more than one month or year query string in the url. The solution will be to implement your own logic to filter blog posts by multiple month and year pairs.

TIP: You must specify the UrlEvaluationMode of the Blog posts control to QueryString from the advanced settings in the designer.

  1. Get the base url of the blogs page

2.Build custom date evaluator to override the logic from Sitefinity's DateEvaluator.cs to build url with month and year as query strings:

3.Resolve the url by appending query strings based on the checkboxes checked in the custom Archive control:

4.Hook to the filter button click event to get the checked checkboxes and Response.Redirect the page to append the query string parameters:

5.Preserve checked checkboxes after page is redirected

This can be achieved my many ways - storing the checked values in session, cache etc. In this blog posts you will create new class ObjectCache with single property CheckboxesCache that will store the checked checkboxes values:

This symbolized object cache will be invalidated in the start of the filter button click event handler and substituted if any checkbox is checked in the same event handler.

Now you can handle the PreRender event of the Archive repeater and check all checkboxes which text is found in the ObjectCache:

Read the built query string and filter blog posts list

Blog posts list view is built by the MasterPostsView class. You must inherit the class and override several methods to apply the custom filter.

Override the InitializeListView method to filter the blog posts query:

In this method, you read the query string parameters and perform filtering by custom filter criteria. The code below shows how the filter expression is built:


The code above splits the query string values of the year and month query strings. If by some reason the query string pairs month and year are broken - the query will return all blog posts (same as when no query string is applied). Now that you have a specific month and year pair - you must get the last day of the month to construct expression to find all blog posts between the first and the last day of the month.

For example: 
(PublicationDate >= 1/1/2015 AND PublicationDate <= 31/1/2015) OR (PublicationDate >= 1/2/2014 AND PublicationDate <= 28/2/2014)

The following function is used to generate the filter expression:


As a final step you need to register the custom controls that you created. See the next step.

Register custom controls and configure them

1.Register CustomArchiveControl

Follow the steps in Sitefinity's documentation to register the custom archive control in the Page Toolbox. Once you drag the control on the page make sure to set the control's properties:

ContentType: Telerik.Sitefinity.Blogs.Model.BlogPost 
DateBuiltOptions: YearMonth

2.Register custom MasterPostsView

Go to Administration->Settings->Advanced->ContentView->Controls->BlogPostsFrontend -> Views-> MasterBlogPostsFrontend and replace the ViewType property with the CLR type of your custom view:

For example: SFBlogs.CustomControls.Blogs.Views.CustomMasterPostsView

Save the changes and from this point on all Blog posts widgets on your site will use the custom master view instead of the default one.


You can now filter your blog posts by multiple date values based on when the posts are published. You can also apply filters to other fields and not only PublicationDate. Even more you can apply the same logic for other content types as News, Events etc. Just use the code from the post as a basis.

Back To List

By continuing to use this website you are agreeing to its use of cookies. To find out more, please see our cookie policy.