The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ProjectTemplateKey does not work in Java API for Jira Server

Nhat Nam Nguyen
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 21, 2022

Hello,

I need to programmatically create a project via the Java API (version 8.22.5) for Jira Server. Here is my code:

 

// create a new Jira project with inputs from the requester

ApplicationUser projectLeadFinal = userManager.getUserByKey(projectLead.getKey())

def projectDataBuild = new ProjectCreationData.Builder()

//These all worked fine

projectDataBuild.withName(projectName)

projectDataBuild.withKey(projectKey)

projectDataBuild.withType("software")

projectDataBuild.withAssigneeType(AssigneeTypes.PROJECT_LEAD)

projectDataBuild.withLead(projectLeadFinal)

// The problem is here!!!
switch
(projectType) {

    case "Scrum":

        projectDataBuild.withProjectTemplateKey("com.pyxis.greenhopper.jira:gh-simplified-scrum-classic")

        break

    case "Kanban":

        projectDataBuild.withProjectTemplateKey("com.pyxis.greenhopper.jira:gh-simplified-kanban-classic")

        break

    default:

        break

}

ProjectCreationData projectData = projectDataBuild.build() as ProjectCreationData

Project newProject = projectManager.createProject(projectLeadFinal,projectData)
These functions withName, withKey, withType, withAssigneeType, withLead all worked successfully. However, only the function withProjectTemplateKey() didn't work. For some context, if my client inputted "Scrum", I have to create a Scrum template project, if my client inputted "Kanban", I have to create a Kanban template project. I did some research and found these project template keys in Jira Cloud (latest API version) and thought it would be similar in Jira Server: 
Capture1.PNG
I followed this photo but the function withProjectTemplateKey() still didn't work. The project was created successfully but its template remained the default template , not scrum or kanban, in every case. Any ideas how to fix this? Thank you!

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
PD Sheehan
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 Champions.
July 22, 2022

Searching through the JavaDocs, I found: ProjectTemplateManager

You can output the keys for your environment with

import com.atlassian.jira.project.template.ProjectTemplateManager
def ptm = ComponentAccessor.getComponent(ProjectTemplateManager)
ptm.projectTemplates.collect{"$it.name = $it.key.key"}.join('<br>')

But then, rather than hardcoding the key, you could just look it up dynamically

import com.atlassian.jira.project.template.ProjectTemplateManager

def ptm = ComponentAccessor.getComponent(ProjectTemplateManager)
def projectTemplate = ptm.projectTemplates.find{it.name.startWith(projectType)}.key

/* ... */

def projectDataBuild = new ProjectCreationData.Builder()
projectDataBuild.withName(projectName)
projectDataBuild.withKey(projectKey)
projectDataBuild.withType("software")
projectDataBuild.withAssigneeType(AssigneeTypes.PROJECT_LEAD)
projectDataBuild.withLead(projectLeadFinal)
projectDataBuild.withProjectTemplateKey(projectTemplate.key)