I'm creating an SPFx web part that reads tasks from one project file stored in Project Server, gets some values from those tasks (including the values of some local custom fields), and then creates new tasks in a different project on Project Server, in which the values of the local custom fields are stored in enterprise custom fields.
Because I couldn't get everything I needed to do working using the REST API directly, I resorted to using the Project Server JSOM library (PS.js
), which I was able to get working in SPFx.
Now I'm able to create new tasks in the project using the JSOM syntax, which includes creating a new PS.TaskCreationInformation
object, setting some properties on that object, and then adding that to the task collection in a draft project. However, the options for what properties you can set on a TaskCreationInformation
object are limited. So I need to subsequently update the task's enterprise custom fields with the correct values.
I can't seem to find any information on how to do this. All of the things I've found online about "updating enterprise custom fields" are all about updating what I would consider the field definition on the project itself, not about updating the values for a particular task.
Here's the code I have so far:
private createDraftTasks = (tasks: any[]): Promise<any[]> => {
return new Promise<any[]> ((resolve, reject) => {
if (tasks.length > 0) {
const task = tasks.shift();
const newTaskInfo = new PS.TaskCreationInformation();
newTaskInfo.set_name(task.Name);
newTaskInfo.set_start(task.Start);
newTaskInfo.set_finish(task.Finish);
newTaskInfo.set_duration(task.Duration);
const draftTask = this.draftProj.get_tasks().add(newTaskInfo);
this.psCtx.load(draftTask);
this.psCtx.executeQueryAsync(() => {
// i figure this is where i would need to add the values, since
// i can't really do it on the TaskCreationInformation object, can I?
// or is there a way to do it that way?
// i've tried setting it this way, but this doesn't seem to work:
draftTask.MyEnterpriseCustomField = task.localCustomFieldValue;
// and I've tried setting it this way, and this defintiely throws an error:
draftTask.set_MyEnterpriseCustomField(task.localCustomFieldValue);
// also with camelCasing the field name, still throws an error:
draftTask.set_myEnterpriseCustomField(task.localCustomFieldValue);
const updateJob = this.draftProj.update();
this.psCtx.waitForQueueAsync(updateJob, 5, (status) => {
if (status === 4) {
this.createDraftTasks(tasks).then((result: any[]) => {
resolve([draftTask, ...result]);
}).catch(err => {
console.log(`fail after task "${task.Name}"`, err);
});
} else {
console.log(`update fail after "${task.Name}" with code ${status}`);
}
});
}, (sender, args) => {
console.log(`task "${task.Name}" fail`);
console.log(sender);
console.log(args);
});
} else {
resolve([]);
}
});
}
What is the correct Project Server JSOM syntax for setting the value of an enterprise custom field on a particular task?