none
Using CSOM to update multiple tasks with only 1 project publish RRS feed

  • Question

  • Hello,

    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 am sure (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.

    I would really appreciate if someone can please point me into the right direction here.

    Tuesday, November 17, 2015 2:02 PM

All replies

  • You should simply move out the update and publish to outside of and after the loop. No need to update and publish every iteration.

    Edit:

    I'm also pretty sure you don't need this:

    projectContext.Load(draftProject.Task);
    projectContext.ExecuteQuery();
    Loading the DraftTaskCollection should be enough.


    Thursday, November 19, 2015 10:11 AM