Stand with Ukraine 🇺🇦

Show Labels for Space — Confluence Cloud Macro

confluence-content

The macro constructs a CQL query to retrieve labeled content and executes it. If results are found, it displays the labels as clickable links.

Try for free

User Parameters

Space Key Param

Select a space

Exclude Label

Enter a label or comma-separated labels to be excluded

Template

#set($spaceKey = $parameters.spaceKey)

#if($spaceKey.equals("currentSpace()"))
  #set($spaceKey = $space.key)
#end

#set($excludeLabelParam = "")
#if ($parameters.excludeLabel)
  #set($excludeLabelParam = $parameters.excludeLabel)
#end

## Prepare exclude labels as a list
#set($excludeLabels = [])
#foreach($label in $StringUtils.split($excludeLabelParam, ","))
#set($discard = $excludeLabels.add($StringUtils.trim($label)))
#end

## CQL query to get all labeled content in the space
#set($cqlQuery = "type IN (page, blogpost, attachment) AND space='$spaceKey'")

## Apply exclude logic if there are labels to exclude
#if($excludeLabels)
#set($excludeCondition = "label='" + $StringUtils.join($excludeLabels, "' OR label='") + "'")
#set($cqlQuery = "$cqlQuery AND NOT ($excludeCondition)")
#end

## Execute the query and expand metadata labels
#set($labelResponse = $ConfluenceManager.get("/wiki/rest/api/content/search?cql=$cqlQuery&expand=metadata.labels"))

## Render all labels as clickable links
#if($labelResponse.results.size() > 0)
<ul>
    <p style="margin-bottom: 10px;">
        <span class="aui-lozenge aui-lozenge-subtle aui-lozenge-success">Search result:</span>
    </p>
    #foreach($content in $labelResponse.results)
    #foreach($label in $content.metadata.labels.results)
    <li style="list-style-type: circle; margin-left: 30px;">
        <a href="$labelResponse._links.base$content._links.webui" target="_blank">$StringUtils.capitalize($label.name)</a>
    </li>
    #end
    #end
</ul>
#else
<p>
    No labels found in the specified space or based on the exclusion criteria
</p>
#end