3

I have the following code that updates certain task fields. The problem with this code is that I have to publish an entire project after updating each task one-by-one. So for example, if I had 500 tasks to update, the project would have to be published 500 times. As you can see, this is completely overkill, slow and unnecessary.

using (ProjectContext projectContext = GetClientContext(psCred))
{
    projectContext.Load(projectContext.Projects);
    projectContext.ExecuteQuery();

    foreach (var project in projectContext.Projects)
    {
        var draftProject = project.CheckOut();
        projectContext.Load(draftProject.Tasks);
        projectContext.ExecuteQuery();

        projectContext.Load(draftProject.Task);
        projectContext.ExecuteQuery();

        foreach (var task in draftProject.Tasks)
        {
            task["FIELD"] = "VALUE";

            var job = draftProject.Update();
            projectContext.WaitForQueue(job, int.MaxValue);

            job = draftProject.Publish(true);
            projectContext.WaitForQueue(job, int.MaxValue);
        }
    }
}

I hope there is a way to update update all of the project tasks at once with only one publish at the end just like how Microsoft Project desktop application does it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Moon
  • 33,439
  • 20
  • 81
  • 132

1 Answers1

0

For what it's worth, I was able to create a bunch of tasks and then use this code to update a custom field on all of the new/existing tasks with a single publish:

DraftProject projCheckedOut = proj2Edit.CheckOut();

_projContext.Load(projCheckedOut.Tasks, ts => 
    ts.Include(
        t => t.Id, 
        t => t.Name, 
        t => t.CustomFields, 
        t => t.OutlineLevel,
        t => t.IsSummary));
_projContext.ExecuteQuery();

// Lookup custom field internal name from our StringDictionary
string intNamePref = _customFields["Custom Field1"];

var tasks = projCheckedOut.Tasks;

foreach (var t in tasks)
{
    t[intNamePref] = 2;
}
projCheckedOut.Publish(true);
_projContext.ExecuteQuery();

Tip, to get the .Include lambda to work, I had to add this using:

using Microsoft.SharePoint.Client;
Chris Snyder
  • 958
  • 4
  • 10
  • Yes it will work for custom fields but non of the out of the box fields get updated thus way. – Moon Dec 03 '15 at 19:48