Reputation: 1
I tried to extract specific task from particular project to update the Resource/ Assignment. The following sample code is for my testing. I perform this in Console .NET framework and no error. Unfortunately, no changes happened in the Task assignment. The scenario will be, Task A was assigned to any personal named PERSON A, and now I want to add or change to another person named PERSON B. I have tried a lot of workarounds on this issue. But never work. If you have any solutions, please let me know, that will save more time for me. Thanks a lot once again.
// Load the draft project
var draftProject = project.Draft;
context.Load(draftProject);
context.ExecuteQuery();
// Load the tasks and project resources
context.Load(draftProject.Tasks);
context.Load(draftProject.ProjectResources);
context.ExecuteQuery();
// Find the specific task
var task = draftProject.Tasks.FirstOrDefault(t => t.Name == Params.PWA_TASK_NAME);
if (task != null)
{
// Load the assignments for the task
context.Load(task.Assignments);
context.ExecuteQuery();
// Load the Resource property for each assignment
context.Load(task.Assignments, assignments => assignments.Include(a => a.Resource));
context.ExecuteQuery();
// Find the assignment
var assignmentToUpdate = task.Assignments.FirstOrDefault(a => a.Resource.Name == "000 New Local Resource");
if (assignmentToUpdate != null)
{
// Load the resources with their Ids and Names
context.Load(draftProject.ProjectResources, resources => resources.Include(r => r.Id, r => r.Name));
context.ExecuteQuery();
// Find the resource
var resourceToUpdate = draftProject.ProjectResources.FirstOrDefault(r => r.Id == assignmentToUpdate.Resource.Id);
if (resourceToUpdate != null)
{
// Display the resource name
Console.WriteLine("Resource Name: " + resourceToUpdate.Name);
// Update the resource name
resourceToUpdate.Name = Params.PWA_LOCAL_RESOURCE_NAME_4;
context.ExecuteQuery(); // 提交更改到服务器
// Save changes to the draft project
draftProject.Update();
context.ExecuteQuery(); // 提交更改到服务器
}
else
{
Console.WriteLine("Resource not found.");
}
}
else
{
Console.WriteLine("Assignment not found.");
}
}
else
{
Console.WriteLine("Task not found.");
}
Try to know what the root cause is. or how to resolve this.?
Upvotes: 0
Views: 9