Categories list to only display categories with documents assigned to them?

uzair Ahmed asked on April 26, 2018 12:04

Hello Team,I am running into a problem. Actually i have different categories," Show all, portfolio1, portfolio2, portfolio3, Portfolio4". I have assigned different document to different categories, its working fine. but i want to display only Category that contains documents. For example if portfolio1 have a document and other have not. then only "Show All,Portfolio1" should be display on Page. How i will do it?? Please Explain in detail

Recent Answers


Dragoljub Ilic answered on April 26, 2018 12:26

Hi Uzair,

You need to take a look into CMS_DocumentCategory table, which keeps refrence between documents and categories. So if you are using simple repeater to show documents on your page, and you used kentico category selector to select category on the page, something like this should work for you when you add it in WHERE condition directly in repeater: DocumentID IN (SELECT DocumentID FROM CMS_DocumentCategory WHERE CategoryID = 4)

Where 4 is category ID that you want to filter on.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 26, 2018 12:33 (last edited on April 26, 2018 14:10)

This is a standard behavior. Categories list web part is using standard repeater underneath. You can clone the web part and change the query it is using or you can just use query repeater the web part instead and use a different SQL query with grouping to get the categories the way you want.

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 12:44 (last edited on April 27, 2018 12:22)

?????????????

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on April 26, 2018 13:30 (last edited on April 26, 2018 13:31)

Hi Uzair,

I'm not sure what is connection between your documents and categories that you are showing there, but can you try to add this query in Where Condition of CategoryList webpart (in your screenshot,field marked as ???): CategoryID IN (SELECT CategoryID FROM CMS_DocumentCategory)

This means, that you will show only categories that are assigned to some document.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 14:04

its Not working :(

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 26, 2018 14:55 (last edited on April 26, 2018 14:57)

try this WHERE condition: (EXISTS(SELECT categoryid FROM cms_documentcategory as dc where dc.CategoryID = CMS_Category.CategoryID))

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 15:30

@Peter Mogilnitski Its also not working. Let me show you whole story. i'll send you Screencast video in which i explain the things

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 15:35 (last edited on April 26, 2018 16:10)

This is ScreenShot video this will explain:-


I am waiting For solution.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 26, 2018 15:47

Cool video :). Yeah you are using custom document field for categories. Category list web does not work with custom fields. You need to assign categories to document in way Kentico way other wise use query repeater web part and write your own query that will check the content of your custom document field.

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 15:52

ohhhhh then what will be the query ? i am totally confused actually

0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 26, 2018 16:12

hello there ????

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 27, 2018 02:33 (last edited on April 27, 2018 13:49)

Ok, you can assign categories to a page on the Properties tab - categories tab and on the form tab. Both assignments are correct - it is just 2 different views on the same thing, i.e. if you change something on categories tab - it will be reflected on the form tab and vice versa. You did assign on the form tab - it is fine! If you switch between form tab and properties -> categories - should see the same list of categories.

So what was said above about WHERE is correct, but I have an idea why it still showing all the categories:

The reason it is showing all categories because it does it globally. i.e. yes your document is not assigned to a given category but some other documents on your site are assigned to this category, so you need to get documents and categories from given part of your tree.

Here is the global query that tells how many document assigned to each category on all of the sites:

SELECT CMS_Category.CategoryID, CMS_Category.CategoryDisplayName, CMS_Category.CategoryName, count(DocumentID) as NumberOfAssignedDocuments
FROM CMS_Category INNER JOIN CMS_DocumentCategory ON CMS_Category.CategoryID = CMS_DocumentCategory.CategoryID 
WHERE DocumentID IN (SELECT DocumentID FROM View_CMS_Tree_Joined) 
GROUP BY CMS_Category.CategoryID, CMS_Category.CategoryDisplayName, CMS_Category.CategoryName

The condition that I mention above needs some tuning, i.e. you dont care about all document/categories on your site you care only about specific subset of your documents for example documents under /specialpages/ and categories for this subset of documents - lets restrict th categories:

(EXISTS(SELECT categoryid FROM cms_documentcategory as dc
JOIN View_CMS_Tree_Joined vw on dc.DocumentID = vw.DocumentID and NodeAliasPath like '/SpecialPages/%'
 where dc.CategoryID = CMS_Category.CategoryID))
0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 27, 2018 09:38

Getting this Error :- https://www.screencast.com/t/xNnleeSh ... i have tried this :-

< location allowOverride="true">

< system.web> < trust level="Medium" originUrl="" /> </system.web>

< /location>

But its not working.. I think There is some issue in Query .. What do you say? and thankyou very much for your Precious Time.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 27, 2018 13:41 (last edited on April 27, 2018 13:44)

yep parentheses are incorrect

(EXISTS(SELECT categoryid FROM cms_documentcategory as dc
JOIN View_CMS_Tree_Joined vw on dc.DocumentID = vw.DocumentID and NodeAliasPath like '/SpecialPages/%'
 where dc.CategoryID = CMS_Category.CategoryID))
0 votesVote for this answer Mark as a Correct answer

uzair Ahmed answered on April 27, 2018 14:45

opsss... It hides All Categories :/

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 27, 2018 16:01 (last edited on April 27, 2018 17:46)

So the query is working then! I assume you've specified correct NodeAliasPath? And documents in this sub tree section (for this NodeAliasPath) have any categories assigned to them?

0 votesVote for this answer Mark as a Correct answer

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