Show Labels for Space — Confluence Cloud MacroNew
This Confluence macro constructs a CQL query to retrieve labeled content and executes it. If results are found, it displays the labels as a table or a list with clickable links.
Template
#set($spaceKey = $space.key)
#if($parameters.space && !$parameters.space.equals("currentSpace()"))
#set($spaceKey = $parameters.space)
#end
#set($viewMode = "table")
#if($parameters.viewMode && $parameters.viewMode != "")
#set($viewMode = $parameters.viewMode)
#end
#set($excludeList = [])
#if($parameters.excludeLabels && $parameters.excludeLabels != "")
#foreach($ex in $parameters.excludeLabels.split(","))
#set($discard = $excludeList.add($ex.trim()))
#end
#end
## Collect labels from actual content via CQL
#set($labels = {})
#set($cql = "type IN (page,blogpost,attachment) AND space='${spaceKey}'")
#set($s = 0)
#set($l = 200)
#set($go = true)
#foreach($i in [1..20])
#if($go)
#set($r = $ConfluenceManager.get("/wiki/rest/api/content/search?cql=${cql}&expand=metadata.labels&start=${s}&limit=${l}"))
#foreach($c in $r.results)
#foreach($lb in $c.metadata.labels.results)
#set($n = $lb.name)
#if(!$excludeList.contains($n))
#if($labels.containsKey($n))
#set($cur = $labels.get($n))
#set($cur = $cur + 1)
#set($discard = $labels.put($n, $cur))
#else
#set($discard = $labels.put($n, 1))
#end
#end
#end
#end
#if($r.results.size() < $l)#set($go = false)#else#set($s = $s + $l)
#end
#end
#end
## Early return if nothing found
#if($labels.size() == 0)
<div>
No labels found in this space 🔍
</div>
#stop
#end
## Render as unordered list
#if($viewMode == "list")
<ul>
#foreach($n in $labels.keySet())
<li>
<a href="${baseUrl}/search?cql=label='${n}' AND space='${spaceKey}'">$n</a>
<span style="color: #707070; margin-left: 4px;">($labels.get($n))</span>
</li>
#end
</ul>
## Render as table (default)
#else
<table class="aui aui-table-sortable" style="width:50%;">
<thead>
<tr>
<th style="width: 50%; padding: 8px 12px;">Content count</th>
<th style="width: 50%; padding: 8px 12px;">Label</th>
</tr>
</thead>
<tbody>
#foreach($n in $labels.keySet())
<tr>
<td style="padding: 8px 12px; text-align:left;">$labels.get($n)</td>
<td style="padding: 8px 12px;">
<a href="${baseUrl}/search?cql=label='${n}' AND space='${spaceKey}'">$n</a>
</td>
</tr>
#end
</tbody>
</table>
#endUser Parameters
Space
Choose a space
View Mode
Choose how to display results
Exclude Labels
Enter a label or comma-separated labels to exclude
You May Also Like
Displays a list of pages in specific space with certain title or label
Macro for printing all the spaces
Space Information macro by space Id
Display a list of Confluence pages with CSV download feature
Shows all attachments from the current space in a table view
Generate a list of labels from all spaces leading to corresponding content pages, organized in alphabetical order.
Shows H1 headings directly to children's topics for easy navigation.
Display information about how many users have access to each space
Shows page creation date
Display Jira issue comments in a table