Tuesday, 21 April 2020

Update Custom Column of Task in Project Online using JSOM

var projContext;
var projects;
var project;
var draft_project;
var tasks;
var task;
var customfields;
var ProjectId = '<guid here>'; //Guid of the Project to Be Updated

SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "PS.js"); //Load PS.js

function runMyCode() {
 //Create the ProjectContext object
 projContext = new PS.ProjectContext('<pwa site>');

 //Project List information will be placed inside projects variable
 projects = projContext.get_projects();

 //Signal the information that is going to be loaded
 projContext.load(projects);

 //Send the request to the server
 projContext.executeQueryAsync(LoadProject, QueryFailed);
}

function LoadProject() {
 //Create variable that will hold specific project information
 project = projects.getById(ProjectId);
 projContext.load(project);
 projContext.executeQueryAsync(ProjectLoadFinished, QueryFailed);
}

function ProjectLoadFinished() {
 //The project Variable contains published information
 // alert(project.get_name());

 //In order to change the project you need to Checkout the Project
 draft_project = project.checkOut();
 GetTasks();
}

function GetTasks() {
 tasks = draft_project.get_tasks();
 projContext.load(tasks);
 projContext.executeQueryAsync(TasksLoadFinished, QueryFailed);
 //var jobUid = draft_project.update();
 //projContext.waitForQueueAsync(jobUid, 3600, PublishAndCheckIn);
}

function TasksLoadFinished() {
 task = tasks.getByGuid('<task guid>'); //GUID of Task
 projContext.load(task);
 projContext.executeQueryAsync(TaskLoadFinished, QueryFailed);
}

function TaskLoadFinished() {
 //customfields = task.get_customFields();
 //projContext.load(customfields);
 task.set_item('<custom Column internal name>', 'This is a testing method'); //set value here
 var jobUid = draft_project.update();
 projContext.waitForQueueAsync(jobUid, 3600, PublishAndCheckIn);
 //projContext.executeQueryAsync(CustomFieldsLoadFinished, QueryFailed);
}

function CustomFieldsLoadFinished() {
}

function PublishAndCheckIn(response) {
 Log("Publishing and Checkin in");
 var jobUid2 = draft_project.publish(true);
 projContext.waitForQueueAsync(jobUid2, 3600, FinalMessage);
}

function FinalMessage(response) {
 console.log(response);
}

function QueryFailed(error) {
}
});


For Lookup values update is as follows:
To get the internal name of a Task Custom Field below is the endpoint.
Note: This code will update the project plan without going to approval center

No comments:

Post a Comment