Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Confluence user macro to check if page exists

Pavel Potcheptsov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2016

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

2 votes
Answer accepted
Marcel Woschek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2016

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

Pavel Potcheptsov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2016

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?

Marcel Woschek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2016

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.

1 vote
Marcel Woschek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2016

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
TAGS
AUG Leaders

Atlassian Community Events