Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

How to identify a space given a pageID.

Due to the format of our page titles, our page URLs always resolve to ../pages/viewpage.action?pageId=########

And people are constantly receiving a URL to a page that results in the error: You don't have permission to view this page. 

This results in numerous tickets to our instance admin to determine the space/admins of the page.

It looks like I could possibly write a user macro to get ancestors (which also might not be viewable by the user) so what I really need is to identify the space or the page admins based on the pageId.

2 answers

4 votes

Are you looking for a way for your help desk to look up the space based on the page id or are you looking to show that to the user in the permissions message when it shows up?

All the options are interesting, I would like to get the name of the space to grant access to this page.

com.atlassian.confluence.pages.Page page = pageManager.getPage(pageId);
com.atlassian.confluence.spaces.Space space = page.getSpace();

getPage(long id) - Deprecated.
since 7.3.0, we can use ContentService.find(Expansion...) from plugins

com.atlassian.confluence.api.model.content.Content content = contentService.find(new Expansion("space")).withId(ContentId.of(pageId)).fetchOrNull();
com.atlassian.confluence.api.model.content.Space space = content.getSpace();

How can we insert that into a user macro ?

Like Greg Green likes this

^^^^ 100% this ^^^^

inside a macro:

pageId = conversionContext.getEntity().getId();

gotti_p :  A one-line macro...  Not really helping at all.

Where do you put the pageID in that ?  In the getId(<pageID>) ?

The long pageId variable will contain the space ID ?  Not clear or maybe you misread the initial question.

thought the rest was clear from the code before

inside the macros execute-method you get the pageId and with that you get the page and the space, etc


public String execute(Map<String, String> params, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException {
String body = conversionContext.getEntity().getBodyAsString();

long pageId = conversionContext.getEntity().getId();

Page page = pageManager.getPage(pageId);
long spaceId = page.getSpace().getId();


pageManager you get by injection similar to all other components you might need:


public MyMacro(@ComponentImport PageManager pageManager,
@ComponentImport SpaceManager spaceManager,
) {
this.pageManager = pageManager;
this.spaceManager = spaceManager;

If the question is about macros in general here is a sample:

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events