Stand with Ukraine 🇺🇦

Get Content by Label — Confluence Cloud Macro

confluence-content

The macro processes user-defined parameters to construct a CQL query for searching Confluence content.

Try for free

User 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

123

Ancestor ID

Enter a top-level page ID to narrow search results to a specific page tree

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>
#end