Create Tasks (Summary and Sub tasks) in a schedule on a button click using JSOM

Hello Readers !

In this blog post, we are going to learn how to provision tasks in a schedule (or) how to create Summary tasks and sub tasks on click of a custom button which is on top of the schedule pdp using JSOM. Please note that this code will work only in the Browser mode not in the client application. If you are looking for a functionality to create tasks during project creation, you may need to follow other processes like the combination of Power Automate & Azure functions (or) you can do this using Azure web App CSOM code when a new project gets created etc.. and there are many other ways too.

But this solutions is, specifically for the browser based upon click of a custom button. This is a basic code which creates just 4 summary tasks and 4 subtasks under each summary tasks also updates the sub tasks duration as well. You can take this as a reference and enhance this as per your requirements.

 

Add the below code as it is into a .js file and upload it to site Assets folder then link this file to a content editor webpart (CEWP) on the schedule pdp. This is a verified code in my Project online tenant.

Code snippet:

</pre>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script type="text/javascript" src ="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src ="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/ps.js"></script>

<div> <input type="button" id="btnCreateTasks" class="createTasks" value="Provision Schedule" /> </div>

<script type="text/javascript">

$(document).ready(function () {
var project_name = PDP_projName;
var projUid = PDP_projUid;
var summaryTasks= ['Summary 1','Summary 2','Summary 3','Summary 4'];
var subTasks= ['Task 1','Task 2','Task 3','Task 4'];
var project,tasks,projContext,projects;

$('#btnCreateTasks').click(function(){
//disable the button to avoid duplicate clicks.
$(this).attr('value', 'Creating Schedule...').prop('disabled', true);

// Project Service starts here
projContext = PS.ProjectContext.get_current();
projects = projContext.get_projects();
project = projects.getByGuid(projUid).checkOut(); //get_draft();
projContext.load(project);
projContext.executeQueryAsync(GetDraft, QueryFailed);
});

function GetDraft(){
console.log(project.get_name());
tasks = project.get_tasks();
projContext.load(tasks);
projContext.executeQueryAsync(CreateTasks, QueryFailed);

}
function CreateTasks(){
// Create Summary tasks in the project
var previousTaskId;
for (let j = 0; j < summaryTasks.length ; j++) {
var task_name = summaryTasks[j];
var summary_task_id = NewGuid();
var new_task = new PS.TaskCreationInformation();
new_task.set_name(task_name);
if(j>0){
new_task.set_addAfterId(previousTaskId);
}
new_task.set_id(summary_task_id);
tasks.add(new_task);

//Create subTasks
for (let i = 0; i < subTasks.length ; i++) {
var task_id = NewGuid();
task_name = subTasks[i];
var sub_task = new PS.TaskCreationInformation();
sub_task.set_name(task_name);
sub_task.set_duration('5d');
sub_task.set_id(task_id);
sub_task.set_parentId(summary_task_id);
tasks.add(sub_task);
previousTaskId = task_id;
}
}
var queueJob = project.publish(true); //project.update();
projContext.waitForQueueAsync(queueJob, 100, WaitForQueueCallback);
}
function QueryFailed(error) {
alert("There's an error while fetching tasks info. Please try again after checkin the project if it's in checked out");
}

/* Callback for success/failure */
function WaitForQueueCallback(job_state) {
switch (job_state) {
case PS.JobState.success:
alert("Tasks have been created Successfully");
location.reload();
break;
case PS.JobState.readyForProcessing:
case PS.JobState.processing:
case PS.JobState.processingDeferred:
alert("Creating the Tasks is/are taking longer than usual.");
break;
case PS.JobState.failed:
case PS.JobState.failedNotBlocking:
case PS.JobState.correlationBlocked:
alert("Failed to create tasks. The creation job is in state " + job_state);
break;
default:
alert("Unknown error, job is in state " + job_state);
}
}

// Function to generate GUIDs (https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript/2117523)
function NewGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
}

});
</script>
<pre>
Thank you for reading the post.
Summary
Create Tasks (Summary and Sub tasks) in a schedule on a button click using JSOM
Article Name
Create Tasks (Summary and Sub tasks) in a schedule on a button click using JSOM
Description
Create Tasks (Summary and Sub tasks) in a schedule on a button click using JSOM. Learn how to provision tasks in a schedule (or) how to create Summary tasks and sub tasks on click of a custom button which is on top of the schedule pdp using JSOM
Author