How to identify a space given a pageID.

John Corwith August 28, 2020

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.

3 answers

4 votes
Davin Studer
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 28, 2020

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?

Fred Kruger January 26, 2021

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

1 vote
Dmitry January 14, 2022

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

Dmitry January 14, 2022

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();

Deleted user March 7, 2022

How can we insert that into a user macro ?

Like Greg Green likes this
Greg Green
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 8, 2022

^^^^ 100% this ^^^^

gotti_p
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 15, 2022
inside a macro:

long
pageId = conversionContext.getEntity().getId();
Deleted user July 16, 2022

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.

gotti_p
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 17, 2022

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:

constructor:

@Autowired
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:

https://developer.atlassian.com/server/framework/atlassian-sdk/create-a-confluence-hello-world-macro/

0 votes
Hendrik Huerkamp
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 3, 2024

If you just need to get the Space and the linked page in human readable for one time (without Macro etc.) you just could try to open that page and look into the Audit log, cause a request for a restricted page, where you don´t have the needed rights will result in a audit log listing. In the audit log the space and respective page is linked.

hope that helps.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events