Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Confluence User Macro to check whether there's content with a label

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 9, 2014

Hi guys,

I have a number of custom user macros that rely on the contentbylabel macro to generate lists of pages. One thing that is annoying, however, is that if there are no pages that have a certain label attached to them then an error message is returned stating that no content can be found. I'd prefer it if nothing was shown at all.

I'd like to be able to wrap these contentbylabel macros in a condition that first checks whether any results exist and, if so, then run the contentbylabel macro. Alternatively, a smarter solution (so that two checks aren't performed) might be to replicate the behaviour of the contentbylabel macro and then build an array out of the results. If there are results then output them, else do nothing.

I'm a bit lost when I look at the Confluence API docs so does anyone know where I should be looking (or, better, could give me some sample code that achieves this)?

Thanks muchly

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2014

Hi Steve,

I think the best solution would be to check whether the contentbylabel macro shows any results, and then simply not show it if there are none. Using this macro text, you should be able to figure it out.

## @noparams

#set ( $contentByLabelResults = $action.helper.renderConfluenceMacro("{contentbylabel:labels=missing-label}") )
$generalUtil.escapeXml($contentByLabelResults)
#if ( !$contentByLabelResults.contains("No content found for") )
  $contentByLabelResults
#end

As you can see, the renderConfluenceMacro method outputs the results as HTML and stores it in the velocity variable, and the content is displayed on the screen by escaping the HTML tags (next line, which can be removed; only shown for instructive purposes). If there are results (i.e. the "No content found for label(s):" message is not shown), then it will display the results.

If you have any other questions, let me know.

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2014

Excellent. That's simpler than what I had written. Many thanks Stephen.

TAGS
AUG Leaders

Atlassian Community Events