2

I try to do a simple task: with help of Visual Studio I try to get info about tasks such as baseline. I can read and get all projects, but can't get list of tasks. How I can get it? Here the example of my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProjectServer.Client;


namespace ProjectAPI
{
    class Program1
    {
        private const string pwaPath = "https://XXXXXXXXX"; 
        private static string projName = string.Empty;

        private static ProjectContext projContext;
        private static PublishedTask projTasks;

        static void Main(string[] args)
        {
            projContext = new ProjectContext(pwaPath);
            projContext.Load(projContext.Projects);
            projContext.ExecuteQuery();

            List<string> TasksID = new List<string>();

            foreach (PublishedProject pubProj in projContext.Projects)
            {
                Console.WriteLine("\n\t{0} : {1}", pubProj.Name, pubProj.CreatedDate.ToString());
                foreach (PublishedTask item in pubProj.Tasks)
                {
                    TasksID.Add(projTasks.Name);
                                    }
                Console.WriteLine("\nProject ID : Project name : Created date");
            }

            Console.Write("\nPress any key to exit: ");
            Console.ReadKey(false);

        }
    }
}

After that i have following error:

"Microsoft.SharePoint.Client.CollectionNotInitializedException" in Microsoft.SharePoint.Client.Runtime.dll

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236

1 Answers1

0

You need to load the tasks first:

foreach (PublishedProject pubProj in projContext.Projects)
{
   Console.WriteLine("\n\t{0} : {1}", pubProj.Name, pubProj.CreatedDate.ToString());
   projContext.Load(pubProj.Tasks);
   projContext.ExecuteQuery();
   foreach (PublishedTask item in pubProj.Tasks)
   {
      foreach (PublishedTask pubTask in pubProj.Tasks)
      {
         Console.WriteLine("\n{0}", pubTask.Name);
      }
   }
}
Jeff
  • 11
  • 5