Get/display Page ID - Confluence Internal Code
Asked by AccountName:AIB:

I am trying to display a page's ID within itself i.e. within the content of the page the page ID is displayed.

Q/A within the community recommend using **$content.getIdAsString()**but that has not worked. I've tried to input this within a HTML macro and without one as well but to no avail.

I'm trying to get the ID of the page so that an HTML code block could reference the page it is included on instead of inputting the page ID by hand every single time.

Looking forward to your responses.


Answers:
author: Thomas Schlegel
body: Hi,

you have to create a user macro with that content:

```
## Macro title: getpageid
## Macro has a body: N

## @noparams

$content.getIdAsString()
```

Then, put the macro to your page.

If you need help, creating a user macro, have a look at [this article](https://community.atlassian.com/t5/Confluence-articles/The-Admin-s-tale-User-Macro-filtering-a-Confluence-table/ba-p/459369)

comments: author: AccountName:AIB
body: Hi Thomas,

Thanks for the quick reply and much appreciated. I had a hunch (which I was ignoring) that this ought to be put in a macro. In turn, could this macro then be used within an HTML code block?

Here is what my code block looks like right now:

```
<input type="image" src="/download/attachments/171705685/BackReturn.png" onclick="location.href=document.referrer; return false;"> 
<input type="image" src="/download/attachments/171705685/PDFdownload.png" onclick="location.href='/spaces/flyingpdf/pdfpageexport.action?pageId=171706153';">
```

The last pageID (underlined) is what I am trying to get through the code/macro (dynamically) so that it doesn't require manual input every single time. Could it be something like **action?pageId=getpageid**


author: Thomas Schlegel
body: No, you can't use that in an html code block natively. This macro simply returns the id as a string.

But you could change the macro, so that it is not only displaying the id, but returning the whole html-input-tag.

I have to leave now, so I don't have the time to try that out.

I will look at this tomorrow, but maybe you can try it meanwhile on your own. It's not so hard ;-)


author: AccountName:AIB
body: Sounds like a plan. I'll give it a try.

Thanks again :)


author: Thomas Schlegel
body: did you get it on you own?

If not, here's the content of the macro:

```
## @noparams

<input type="image" src="/download/attachments/171705685/BackReturn.png" onclick="location.href=document.referrer; return false;"> 
<input type="image" src="/download/attachments/171705685/PDFdownload.png" onclick="location.href='/spaces/flyingpdf/pdfpageexport.action?pageId=$content.getIdAsString()';">
```


author: AccountName:AIB
body: Hi Thomas,

Thanks for the wonderful initial suggestion. I was able to create a macro that dynamically got the pageID. However, initially I only created the macro with the code you provided but quickly realized that the HTML macro doesn't accept other macros within itself.

Therefore, I included the entire HTML code (including the pageID) within the macro and everything works as intended. All my CSS and HTML elements are now where they should be and the icons onclick events work


author: Markus P?hler
body: Hello Thomas,

we use this user defined makro approach. Within a page property report, where this makro is in the page property "PageID" of the property pages, in some cases more or less ramdomly the resultset just delivers the value "PageID" as text instead of the number we expect to be delivered.

It is very strange and I could not figure out what is happening. Do you have any thoughts about this?

Thx Markus