Working with Office365 Project Online Tasks using JSOM - Part II

Updated: Mar 29, 2019

In this article, let us look at updating or deleting tasks on Office 365 project online using JavaScript Object Model.

This article series focuses on working with office 365 project online plan schedule tasks using JSOM approach. The previous article explains creating or retrieving tasks on project online schedules using JSOM approach.

Update Task on Project Schedule 

  • To update a task on the project online, the following operations are performed. Check-out a project – This operation involves get project by GUID and check-out. (If already checked out, this step is not required) 

  • Update task (OOB/custom fields can be updated) - This operation involves steps like get project by GUID, then get the project draft, get the task collection from the project draft, get task using task GUID, set necessary fields (OOB or custom) with values and updating project draft. 

  • Publish & check-in the project. - This operation involves get project by GUID, get the project draft, publish/check-in the project. (If other actions are involved, this step is not required)

Note: Check-out/Check-in operations are not shown in the piece of code below. If you are performing multiple actions/operations in the project, these actions need not be repeated multiple times. If you want to perform these operations, please refer to the previous article.

For example, assume a task is already created on the project. The following piece of code helps task updating task.

function updateTask(projGuid, taskGuid){

var deferred = $.Deferred();

var context = PS.ProjectContext.get_current();


// Get Task from project using GUID

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var draftProject = project.get_draft()

var tasks = draftProject.get_tasks();

var task = tasks.getByGuid(taskGuid);


// Set OOB/Custom Fields

task.set_duration('18d');

task.set_item('Custom_0652e0806606e911afb600155d48510a','value1')


// Update project

draftProject.update();

context.load(draftProject);

context.load(task);

context.executeQueryAsync(function(){

console.log("Task updated");

console.log(task.get_name());

deferred.resolve(projGuid);

},

function(sender,args){

console.log("update task error");

deferred.resolve(projGuid);

});

return deferred.promise();

}

Delete Task on Project Schedule

  • To delete a task on the project online schedule, the following operations are performed. Check-out a project – This operation involves get project by GUID and check-out. (If already checked out, this step is not required) 

  • Delete task - This operation involves steps like get project by GUID, then get the project draft, get the task collection from the project draft, get task using task GUID, delete the task using deleteObject method and updating project draft. 

  • Publish & check-in the project. - This operation involves get project by GUID, get the project draft, publish/check-in the project. (If other actions are involved, this step is not required)

Note: Check-out/Check-in operations are not shown in the piece of code below. If you are performing multiple actions/operations in the project, these actions need not be repeated multiple times. If you want to perform these operations, please refer to the previous article.

The following piece of code helps deleting a task on the project schedule.

function DeleteTask(projGuid, taskGuid){

var deferred = $.Deferred();

var context = PS.ProjectContext.get_current();


// Gets task by GUID

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var draftProject = project.get_draft()

var tasks = draftProject.get_tasks();

var task = tasks.getByGuid(taskGuid);


// Delete Task

task.deleteObject();


// Update project after deletion

draftProject.update();

context.load(draftProject);

context.executeQueryAsync(function(){

console.log("Task deleted");

deferred.resolve(projGuid);

},

function(sender,args){

console.log("delete task error");

deferred.resolve(projGuid);

});

return deferred.promise();

}

Thus we have seen managing tasks using JSOM approach.