How to get page id in Confluence programmatically?

How to get page id in Confluence programmatically?

3 answers

0 votes

The answer to that depends on where you are starting from!  Imagine asking someone "how do I get to London?".  When they don't know where you are now.

Where is your code running basically?  A script?  A user macro?  A search result function? An add-on that's adding some form of interface (like the admin fuctions)?  Another way to ask that is "in what context"?

To the macro Java code:

<macro name='grid-user-timesheet' class='${project.groupId}.GridEditorUserMain' key='agile-application-stack-user'>
        <description>Creates a editable Timesheet to Confluence Page.</description>
        <resource type="velocity" name="help" location="templates/extra/editor/grideditoruser-help.vm">
            <param name="help-section" value="advanced"/>
        </resource>        
    </macro>

 

So, this is an add-on for Confluence that provides a macro?

From memory, there should be a "context" variable, which gets an id via context.entity.id

Yes this is an Add-on for Confluence.

That looks right to me.  Not tested it, but it should do it.

Hi Matti,

did you figure this one out? I have the same challenge, getting the page ID in a plugin macro. How can I retrieve it in the Java class file?

Best regards,
George

Edit: Sorry, this should have been a reply to the first answer.

Here is how I was able to resolve this:

import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
...

public class MyClass (...
...
public String execute(...
ContentEntityObject contentEntityObject = conversionContext.getEntity();
System.out.println("pageId : " + contentEntityObject.getId());
...
}
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Confluence

What are your project planning tips?

Hello Community,  Jessica here from the Confluence product marketing team! Today I wanted to get your takes on project planning –– what works, what doesn’t, how do you know if you’re doing it r...

154 views 0 3
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