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 edited

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

0 votes
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 Leaders.
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)

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events