It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Cloud : Get existing Page id

Hello people.
I am trying to create Confluence page from Jira Issue Creation with post function.
I have code which works. my problem is that ,i want to create this page as a child page to an existing Page, which i created earlier.

def issueKey = 'PT-6'
def spaceKey = 'PT'

 


def result = get("/rest/api/3/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status != 200) {
return "Error retrieving issue ${result}"
}

 

```
def subtitle = "${result.body.key} - ${result.body.fields.summary}"
def pageContent = """<p>Document date:7/25/2019</p>
<ac:structured-macro ac:name='jira' ac:schema-version='1' ac:macro-id='ab120ae6-a585-4b2f-a713-4d23b355dc17'>
<ac:parameter ac:name='server'>System JIRA</ac:parameter>
<ac:parameter ac:name='columns'></ac:parameter>
<ac:parameter ac:name='serverId'>9cd10b67-ca88-369e-937a-42f902e4289b</ac:parameter>
<ac:parameter ac:name='key'>${issueKey}</ac:parameter>
</ac:structured-macro>"""
def parent = createconfluencePage("Initiation1", spaceKey)


createConfluencePage(subtitle, spaceKey, pageContent, parent)


String createConfluencePage(String pageTitle, String spaceKey, String pageContent, String parentPage) {
def params = [
type : "page",
title: pageTitle,
space: [
key: spaceKey
],
body : [
storage: [
value : pageContent.toString(),
representation: "storage"
]
]

]
if (parentPage != null) {
params["ancestors"] = [parentPage].collect { [id: parentPage.toString()] }
}


def pageResult = post('/wiki/rest/api/content')
.header('Content-Type', 'application/json')
.body(params)
.asObject(Map).body

if (pageResult.statusCode) {
logger.error("Failed to create a new page. Confluence responded with error code: {}", pageResult.statusCode)
} else {
logger.info("Successfully created a new space with id: {}", pageResult)
}
pageResult.id
}

parent

```

 

AS you see i need Page Link or id in 
" def parent = createConfluencePage("Initiation1", spaceKey) " 

How to get it?

1 answer

Hi Dachi,

Thank you for your question.

I can confirm that ScriptRunner for Jira Cloud does not have any built-in functionality to auto get the ID of a page which means you will need to make a call to the Confluence Cloud Rest API in your script which is documented here in order to get the ID for the page you require. 

You could then call the search API located here in order to search the page you require and to return it inside your script so that you can get the ID of it.

If you have created the page previously, then another approach you could look to use is to save the ID of the created page in a text field on your issue when the page is created, so that you can retrieve this value from the issue when it is needed in your script.

I hope this helps.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you