Setting Status Manager on task programmatically

Posted By Posted by: Irfan Rizvi on March 21, 2018

The Status Manager (Task) field in Microsoft Project contains the name of the enterprise resource who is to receive status updates for the current task from resources working in Project Web App.

By default, the status manager is the user who originally published the new task to Project Server. If you are using Project Schedule templates, the default status manager on all the tasks in the template is the person who created the project.

This would not be a problem in most cases but if the person who created the project is not the owner or if the owner had changed, the status manager does not change automatically. And the only way to update the status manager from the schedule interface is by editing the project in Microsoft Project.

The following C# code snippet can be used programmatically to set a status manager on all the tasks:

private static void UpdateStatusManagerForProject(PublishedProject project)
        {
            if (project != null)
            {
                try
                {
                    
                    var draftProject = project.CheckOut().IncludeCustomFields;
                    draftProject.Context.Load(draftProject.Owner);
                    draftProject.Context.ExecuteQuery();
                   
                    foreach (var task in draftProject.Tasks)
                    {
                        draftProject.Context.Load(task.StatusManager);                        
                    }
                    draftProject.Context.ExecuteQuery();
                    foreach (var task in draftProject.Tasks)
                    {
                        try
                        {
                            if (!task.IsSummary)
                            {
                                task.StatusManager = draftProject.Owner;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.Write(string.Format("FIELD SET ERROR:", ex.Message));
                        }
                    }

                    try
                    {
                        draftProject.Publish(true);
                        QueueJob qJob = draftProject.Context.Projects.Update();
                        draftProject.Context.WaitForQueue(qJob, 20);
                    }
                    catch (Exception ex)
                    {
                        Console.Write(string.Format("PUBLISH ERROR:", ex.Message));
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(string.Format("GENERAL ERROR:", ex.Message));
                }
            }
        }

Blog Posted In