Get Content by Label — Confluence Cloud Macro
The macro processes user-defined parameters to construct a CQL query for searching Confluence content.
Template
#set($labelParam = $StringUtils.lowerCase($parameters.label))
#set($excludeLabelParam = "")
#if($parameters.excludeLabel)
#set($excludeLabelParam = $StringUtils.lowerCase($parameters.excludeLabel))
#end
#set($contentType = $parameters.contentType)
#set($specificSearch = $parameters.specificSearch)
#set($spaceKey = $parameters.targetSpaceKey)
#set($ancestorId = $parameters.ancestorID)
## Split labels and excludeLabels by commas into lists and trim each label to avoid spaces
#set($labels = [])
#foreach($label in $StringUtils.split($labelParam, ","))
#set($discard = $labels.add($StringUtils.trim($label)))
#end
#set($excludeLabels = [])
#foreach($excludeLabel in $StringUtils.split($excludeLabelParam, ","))
#set($discard = $excludeLabels.add($StringUtils.trim($excludeLabel)))
#end
## Initialize CQL base query with content type
#if($contentType)
#set($cqlQuery = "type=$contentType")
#else
#set($cqlQuery = "type in (page, blogpost, attachment)")
#end
## Add spaceKey condition if provided
#if($spaceKey)
#set($cqlQuery = "$cqlQuery AND space='$spaceKey'")
#end
#if($ancestorId)
#set($cqlQuery = "$cqlQuery AND ancestor=$ancestorId")
#end
## Build label conditions based on specificSearch parameter
#if($labels)
#if($specificSearch)
## Specific search: join labels with AND condition
#set($labelCondition = "label='" + $StringUtils.join($labels, "' AND label='") + "'")
#set($cqlQuery = "$cqlQuery AND ($labelCondition)")
#else
## Default search: join labels with OR condition
#set($labelCondition = "label='" + $StringUtils.join($labels, "' OR label='") + "'")
#set($cqlQuery = "$cqlQuery AND ($labelCondition)")
#end
#end
## Exclude labels if excludeLabels parameter is provided
#if($excludeLabels)
#set($excludeCondition = "label='" + $StringUtils.join($excludeLabels, "' AND NOT label='") + "'")
#set($cqlQuery = "$cqlQuery AND NOT ($excludeCondition)")
#end
## Execute CQL search request using constructed cqlQuery
#set($response = $ConfluenceManager.get("/wiki/rest/api/content/search?cql=$cqlQuery&expand=metadata.labels"))
## Render search results or message if no results found
#if($response.results && $response.results.size() > 0)
<ul>
#foreach($result in $response.results)
<li style="list-style-type: circle;"> <a href="$response._links.base$result._links.webui" target="_blank">$result.title</a>
<span class="aui-lozenge aui-lozenge-subtle aui-lozenge-success">
#foreach($label in $result.metadata.labels.results)
$label.name
#if($foreach.hasNext) &
#end
#end
</span>
</li>
#end
</ul>
#else
<p>
No results found for the specified criteria.
</p>
#endUser Parameters
Label
Enter a label or comma-separated labels
Exclude Label
Enter a label or comma-separated labels to be excluded
Content Type
Select a content type to search in
Space Key
Select a space key to narrow search results
Specific Search
Check the box to enable an AND search to display only content with both or more labels being set
Ancestor ID
Enter a top-level page ID to narrow search results to a specific page tree
You May Also Like
Generate a list of all the content created by a current user by default or a specified user across your Confluence site.
Shows page creation date
Show confetti bursts and a motivational message upon saving an edited page
Add a configurable floating panel to a Confluence page
Display a custom list of recently updated content
Display the top comments from a selected Confluence page or the current page by default.
Generate a list of labels from all spaces leading to corresponding content pages, organized in alphabetical order.
Retrieve and display labels from a specified space
Parses a table containing cost and currency columns, converts each row's value into a selected result currency, and appends a footer row showing the total sum. Useful for tracking multi-currency expenses and reporting totals in a unified currency.
Improve default Confluence tables with sorting, filtering, pagination, and CSV export features