Working with Office365 Project Online Tasks using JSOM - Part I

Updated: Mar 18, 2019

This article series focuses on working with office 365 project online plan schedule tasks using JSOM approach. The operations like create, retrieve, update or delete project task operations are explained.

Here in this article, let us look at creating or retrieving project schedule tasks using JSOM approach.

Office 365 Microsoft Project Online helps creating project plans, managing schedules and collaborating with other resources virtually. Project plans, task schedules and other objects on the Microsoft Project online can be accessed or created or updated programmatically. Microsoft documentation site provides REST endpoints for working with project online objects.

Load prerequisite files and context:

To get the project server context working, first let us see the prerequisite files to be loaded for getting the project online contexts. The following piece of code shows how the required Project Server/SharePoint files and contexts (PS/SP) are loaded.


$(document).ready(function()

{

// Load Prerequisite files and contexts

SP.SOD.executeFunc('sp.js', 'SP.Utilities.Utility.getLayoutsPageUrl', function(){

SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));

SP.SOD.registerSod('PS.ProjectContext', SP.Utilities.Utility.getLayoutsPageUrl('ps.js'));

SP.SOD.loadMultiple(['SP.ClientContext', 'PS.ProjectContext'], function(){

console.log("All prerequisites loaded");

});

});

});


Create Task on Project Schedule:

To create a task on the project, the following operations are required.

  • Check-out a project – This operation involves get project by GUID and check-out.

  • Add a task with project name and other OOB fields. - This operation involves steps like get project by GUID, then get the project draft, get the task collection from the project draft, set task creation object with OOB field values, add task creation object to the task collection retrieved, and update project draft.

  • Publish & check-in the project. - This operation involves get project by GUID, get the project draft, publish/check-in the project.

Note: Setting a custom field on task creation is not feasible, but a custom field value can be set on the update operation. It will be explained in the later section.

For example, assume a project is already created and available for use. The following piece of code helps understanding task creation steps explained.


function AddTask(projGuid, taskName){


// Check-out project

$.when(CheckOutProjectById(projGuid)).done(function(){


// Create New Task in the project

$.when(newTask(projGuid,taskName)).done(function(){

// Check-in / publish project

CheckInProjectById(projGuid);

});

});

}


// Adds new task to the draft project

function newTask(projGuid, taskName){

var deferred = $.Deferred();


// Retreive the tasks collection

var context = PS.ProjectContext.get_current();

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var draftProject = project.get_draft()

var tasks = draftProject.get_tasks();


// Task Creation

var task = new PS.TaskCreationInformation();

task.set_name(taskName);

task.set_duration('10d');

task.set_start(new Date().toISOString());

var d = new Date();

var e = d.setDate(d.getDate() + 365);


// Add task to the collection

tasks.add(task);


// Update Project

draftProject.update();

context.load(draftProject);

context.load(tasks);

context.executeQueryAsync(function(){


// Task Created successfully

console.log("Task created: "+ task.get_name());

deferred.resolve(projGuid);

},

function(sender,args){

console.log("new task error");

deferred.resolve(projGuid);

});

return deferred.promise();

}


// Check-out project by project GUID

function CheckOutProjectById(projGuid){

var deferred = $.Deferred();

// Get the project

var context = PS.ProjectContext.get_current();

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);


// Check-out project.checkOut();

context.load(project);

context.executeQueryAsync(function(){


// Project checked-out successfully

console.log(project.get_name());

deferred.resolve(projGuid);

},

function(sender,args){


// Error checking-out project

console.log("checkout error");

deferred.resolve(projGuid);

});

return deferred.promise();}


// Check-In Project by GUID

function CheckInProjectById(projGuid){

var deferred = $.Deferred();

// Get Project by GUID

var context = PS.ProjectContext.get_current();

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var draftProject = project.get_draft();


// Publish or Check-in the draft project

draftProject.publish();

draftProject.checkIn();

context.load(draftProject);

context.executeQueryAsync(function(){


// Project checked-in and published

console.log(draftProject.get_name());

deferred.resolve(projGuid);

},

function(sender,args){

// Error check-in project console.log("checkin error"); deferred.resolve(projGuid);

});

return deferred.promise();

}



Retrieve Task from Project Schedule:

The following piece of code shows the getting tasks as collection and getting task by GUID.

// Retrieve Tasks collection by Project GUID

function GetTasks(projGuid){


// Get Task Collection

var context = PS.ProjectContext.get_current();

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var tasks = project.get_tasks()

context.load(tasks);

context.executeQueryAsync(function(){


// Tasks Collection successfully retrieved

var enumerator = tasks.getEnumerator();

while (enumerator.moveNext()) {

var task = enumerator.get_current();

console.log(task.get_id()["_m_guidString$p$0"]);

console.log(task.get_name());

}

},

function(sender,args){ console.log("Get Tasks error");

});

}


// Retrieve Task By Project GUID & Task GUID

function GetTaskById(projGuid,taskGuid){


//Get Task Collection

var context = PS.ProjectContext.get_current();

var projects = context.get_projects();

var project = projects.getByGuid(projGuid);

var tasks = project.get_tasks();

// Get Task

var task = tasks.getByGuid(taskGuid);

context.load(task);

context.executeQueryAsync(function(){


// Task retrieved

console.log(task.get_name());

console.log(task.get_id());

},

function(sender,args){

console.log("Get Tasks error");

});

}


In the next article let us look at updating or deleting project online tasks using JSOM.

Recent Posts

See All