Confluence user macro to check if page exists
Asked by Pavel Potcheptsov:

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


Answers:
author: Marcel Woschek
body: 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

comments: author: Pavel Potcheptsov
body: 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)&lt;ac:macro ac:name="excerpt-include"&gt;  &lt;ac:parameter ac:name="nopanel"&gt;true&lt;/ac:parameter&gt;  &lt;ac:default-parameter&gt;Schedule&lt;/ac:default-parameter&gt;&lt;/ac:macro&gt;#end
```

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


author: Marcel Woschek
body: 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.


author: Marcel Woschek
body: 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)       &lt;ac:macro ac:name="excerpt-include"&gt;           &lt;ac:parameter ac:name="nopanel"&gt;true&lt;/ac:parameter&gt;           &lt;ac:default-parameter&gt;$paramPagename&lt;/ac:default-parameter&gt;       &lt;/ac:macro&gt;#end
```