Hi I am trying to call GET request using Jira Scriptruner's ApplicationLinkRequestFactory.
But I have no idea how to get confluence page template id.
Is there a certain way?
Hi @hojunhwang
To get the Confluence page template ID using ScriptRunner's ApplicationLinkRequestFactory, you need to make a GET request to the Confluence REST API's
/rest/api/template
You can use this endpoint to search for templates by space key or title.
Here's a script that demonstrates how to get a Confluence page template ID using ApplicationLinkRequestFactory:
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)
def templateTitle = "Your Template Title"
def spaceKey = "SPACE_KEY"
if (confluenceAppLink) {
def requestPath = "/rest/api/template?spaceKey=${spaceKey}&title=${templateTitle}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")
def response = request.executeAndReturn(new Response.Handler<String>() {
@Override
String handle(Response response) throws IOException {
response.getEntity(String)
}
})
if (response) {
def jsonResponse = new groovy.json.JsonSlurper().parseText(response)
if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
log.debug("Template ID: ${templateId}")
return templateId
} else {
log.warn("No template found with the given title and space key.")
}
} else {
log.warn("Error: Unable to fetch Confluence templates.")
}
} else {
log.warn("Error: No Confluence application link found.")
}
Make sure to replace
"Your Template Title"
with the title of the Confluence template you're looking for and
"SPACE_KEY"
with the space key containing the template.
This script will search for a template with the specified title and space key and return the template ID if found. If it doesn't find the template, it will log a warning message.
Note that the user account used for the application link needs to have the appropriate permissions in Confluence to access the templates.
Hi @Oday Rafeh
Thanks for the great answer.
If I want default template's id, how should I do? because in this case there is no space id that I can provide. I want to get id of software project space
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want to get the ID of a default template in a software project space, you can use the following script:
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)
def templateTitle = "Your Template Title"
if (confluenceAppLink) {
def requestPath = "/rest/api/template/search?title=${templateTitle}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")
def response = request.executeAndReturn(new Response.Handler<String>() {
@Override
String handle(Response response) throws IOException {
response.getEntity(String)
}
})
if (response) {
def jsonResponse = new groovy.json.JsonSlurper().parseText(response)
if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
log.debug("Template ID: ${templateId}")
return templateId
} else {
log.warn("No template found with the given title.")
}
} else {
log.warn("Error: Unable to fetch Confluence templates.")
}
} else {
log.warn("Error: No Confluence application link found.")
}
Make sure to replace "Your Template Title" with the title of the Confluence template you're looking for.
In this script, I removed the spaceKey parameter and used the
/rest/api/template/search
endpoint instead of
/rest/api/template.
This endpoint allows you to search for templates without specifying a space key. It will search for global templates as well as space-specific ones.
Keep in mind that this script searches for all available templates with the specified title. If multiple templates share the same title, it will return the ID of the first one in the results. Ensure that your search term is specific enough to return the desired template.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Oday Rafeh ,
Thank you for sharing this ScriptRunner script. I tried to use it without any changes (except indentations), but see the following error:
"unable to resolve class Response.Handler<java.lang.String>"
Could you please help me to fix it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
To fix the error in your ScriptRunner script, you can adjust the way you handle the response from the Confluence API. Instead of using Response.Handler
, you can directly get the response as a string. check this simplified version of the script:
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import groovy.json.JsonSlurper
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)
def templateTitle = "Your Template Title" // Change this to your actual template title
if (confluenceAppLink) {
def requestPath = "/rest/api/template/search?title=${URLEncoder.encode(templateTitle, 'UTF-8')}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")
try {
def response = request.execute()
def jsonResponse = new JsonSlurper().parseText(response)
if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
println("Template ID: ${templateId}")
return templateId
} else {
println("No template found with the given title.")
}
} catch (Exception e) {
println("Error: Unable to fetch Confluence templates. ${e.message}")
}
} else {
println("Error: No Confluence application link found.")
}
Make sure to replace "Your Template Title"
it with the actual title of the template you're looking for and let me know please if this helped
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Oday Rafeh ,
Thank you for the solution allowing to parse response directly without handling its headers.
It works.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm glad to hear that @Alfira Merinova and happy to help if anything is needed let me know.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.