Content Filtering with Macro

Brandon R asked on January 31, 2020 20:12

I have a basic repeater and a page data source webpart that is using a page type. My pages and my pagetype items are matched by the same category. I am filtering the data source with the following macro.

{% CurrentDocument.Categories.DisplayNames #%}

This works correctly if my page has a category however if there no category set then all of the items are shown (which make sense because I am not filtering on anything because my macro will display nothing)

What is the best way to fix this issue so that if I don’t set a category then no items are repeated. I am assuming this a quick fix.

Thanks

Correct Answer

Dat Nguyen answered on January 31, 2020 22:32

It's cleaner if you use a ternary operator: {% CurrentDocument.Categories.FirstItem.CodeName.Exists()?CurrentDocument.Categories.FirstItem.CodeName:"none" #%}

Null-coalescing operator might work, too: {% CurrentDocument.Categories.FirstItem.CodeName ?? "none" #%}

1 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on January 31, 2020 20:50

first of all - not so sure about the display name, you probably better off with CodeName. secondly datasource filters based on one category not on many, i.e. {% CurrentDocument.Categories.FirstItem.CodeName %}

There is checkbox in repeater Filter out duplicates, make sure it is checked

0 votesVote for this answer Mark as a Correct answer

Brandon R answered on January 31, 2020 21:03

Peter, I changed the macro, same results.

Each page-type item is different and I had the filter out duplicates all ready selected.

0 votesVote for this answer Mark as a Correct answer

Brandon R answered on January 31, 2020 21:42

I end up writing an if statement that works by putting "none" in the category field. Thanks for your suggestion.

Not sure if their is a better way without having to write an if statement.

{% if ( CurrentDocument.Categories.FirstItem.CodeName != "" )
{CurrentDocument.Categories.FirstItem.CodeName} 
else {"none"} #%} 

Thanks

0 votesVote for this answer Mark as a Correct answer

Brandon R answered on January 31, 2020 21:53

FYI I updated with the exists method instead of checking if the string is empty.

{% if ( CurrentDocument.Categories.FirstItem.CodeName.Exists() ){CurrentDocument.Categories.FirstItem.CodeName} else {"none"} #%}  
0 votesVote for this answer Mark as a Correct answer

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