Confluence user macro to check if page exists

I want to check if page with specific name is exist in current space.

And do something if result in positive.

Use case: template with Excerpt Include macro (with predefined page to display) which have to render only if predefined page exits.

Currently if predefined page isn't exist then Excerpt Include macro throws huge log in atlassian-confluence.log

2 answers

1 accepted

This widget could not be displayed.

Just created an own user macro:

## Macro title: Verify Page
## Macro has a body: Y
##
## Developed by: Marcel Woschek
## Date created: 18/08/2016
## Installed by: Marcel Woschek
##
## @param Pagename:title=pagename|type=string|required=true
##
## Get PageManager object instance
#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($pageManager=$containerContext.getComponent('pageManager'))
 
## Get the Page object instance
#set($page=$pageManager.getPage($space.key,$paramPagename))
#if($page)
       $body
#end

You just need to put the Excerpt Include macro into the body of this macro

Really appreciate your help. It helped to avoid unnecessary log.

I added default value (page name to check) to parameter and Excerpt Include macro with page name to display in case this page exist.

## @param Pagename:title=pagename|type=string|required=true|default=Schedule
## Get PageManager object instance
#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($pageManager=$containerContext.getComponent('pageManager'))
  
## Get the Page object instance
#set ($spacekey=$space.key)
#set($page=$pageManager.getPage($spacekey,$paramPagename))
#if($page)
<ac:macro ac:name="excerpt-include">
  <ac:parameter ac:name="nopanel">true</ac:parameter>
  <ac:default-parameter>Schedule</ac:default-parameter>
</ac:macro>
#end

Could you explain "Get PageManager object instance" code? Is it the way to get access to page name?

You need the PageManager to verify if the page exists in the given space.

$pageManager.getPage($spacekey,$paramPagename) tries to get the $page object using the spacekey and the pagename, because the combination of spacekey and pagename is unique. If the page doesn't exist, this method returns null.

The #if statement then checks if the $page object is not null, hence if the page exists.

This widget could not be displayed.

If you only want to use it for the Excerpt Include Macro you can also integrate it in that selfmade macro:

## Macro title: Excerpt Include if Page Exists
## Macro has a body: N
##
## Developed by: Marcel Woschek
## Date created: 18/08/2016
## Installed by: Marcel Woschek
##
## @param Pagename:title=pagename|type=string|required=true
##
## Get PageManager object instance
#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($pageManager=$containerContext.getComponent('pageManager'))
 
## Get the Page object instance
#set($page=$pageManager.getPage($space.key,$paramPagename))
#if($page)
       <ac:macro ac:name="excerpt-include">
           <ac:parameter ac:name="nopanel">true</ac:parameter>
           <ac:default-parameter>$paramPagename</ac:default-parameter>
       </ac:macro>
#end

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 17, 2018 in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

539 views 7 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you