Custom filter based on categories

Sjoerd de Wildt asked on November 13, 2014 13:44

I was wondering if it is possible to build a custom filter that filters news items in a news list based on their category. It should use the category property as provided by a document out-of-the-box, so without adding a custom category field.

I already got a drop-down list that is databound to these categories and I already read many articles on this. However, I cannot figure out how I could get this category from the drop-down list in the Where-clause, so that the news list only shows the documents that are subscribed to the category that is chosen from the drop-down list.

I have a strong feeling that this should be possible, but I cannot get it done.

Correct Answer

Sjoerd de Wildt answered on November 28, 2014 10:19

I fixed the problem. Below you find the SetFilter method that I now use. The new WHERE condition does the job.

private void SetFilter()
    {
        string where = null;

        if (this.Categories.SelectedValue != null)
        {
            int categoryID = ValidationHelper.GetInteger(this.Categories.SelectedValue, 0);

            if (categoryID > 0)
            {
                where += "DocumentID IN (SELECT DocumentID FROM CMS_DocumentCategory WHERE CategoryID LIKE '%" + this.Categories.SelectedValue + "%')";
            }
        }
    }

Thank you for thinking along and for your time and effort!

0 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on November 13, 2014 22:23

Have you looked at the documentation on the creating custom filter control?

This lays it out pretty well. The only thing you need to change is your datasource, which it sounds like you have done. Then after that you simply create your condition in the SetFilter method, which is called on every postback. What issues are you having if you have followed the custom filter article?

0 votesVote for this answer Mark as a Correct answer

Sjoerd de Wildt answered on November 14, 2014 08:49

First of all, thank you for your answer!

I indeed followed the documentation and the first part works fine. My problem is in the SetFilter method, more precisely in the next line:

where = "SKUDepartmentID = " + departmentId;

I don't know how I can change this line in a way, so that it filters on categories. I guess the problem lies in the fact that one news article can subscribe to many categories. So, I was thinking maybe I should change the CategoryName condition (see image below) instead of the Where condition. I don't know, however, how I can change this property dynamically.

Image Text

0 votesVote for this answer Mark as a Correct answer

Sjoerd de Wildt answered on November 14, 2014 09:00

I'm sorry the image is not working, but I guess this is what I need: Creating a document category filter

In my case, though, I only need to select one category from a drop-down list, which I have already created. However, the same problem here: I cannot get the SetFilter method from this example to work in my code.

0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on November 14, 2014 10:30

Hello Sjoerd,

Images are not working because URL that you have used is point to your local file system... which is of course not available over the Internet. Use some on-line service, e.g. PostImage to upload image on-line and then post the URL of uploaded image and the image will be available for everybody.

Best regards, Martin

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on November 14, 2014 15:23

One thing that you can do is to make sure that you know the database category field. If you have this, then you can store the ids of the categories as the values for your dropdown list and then set a where clause like below:

//drpCategories is the name of the dropdown list
 int categoryId = ValidationHelper.GetInteger(drpCategories.SelectedValue, 0);

 if (categoryId > 0)
 {
    where += "My_CategoryField like '%" + categoryId + "%'";
 }
0 votesVote for this answer Mark as a Correct answer

Sjoerd de Wildt answered on November 21, 2014 10:54

Not exactly what I was looking for, but I guess there is no other way. Thank you for your time and effort!

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on November 21, 2014 16:30

What are you looking for?

0 votesVote for this answer Mark as a Correct answer

Sjoerd de Wildt answered on November 24, 2014 14:18

When you have got a 'News List' you can easily filter the news articles to be displayed based on the category that they belong to; You just select the category in the 'Category Name' property. (see below)

Image Text

However, now I want to dynamically set this field by selecting the category from a drop down list. (so filter my news articles based on their category)

In other words, I want to use the above described functionality (which filters news articles in a news list based on their category) in a filter web part.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.