通过 C# 与 Project Server 2016 集成
本文关键字:2016 集成 Server Project 通过 | 更新日期: 2023-09-27 17:55:14
我尝试做一个简单的任务:在Visual Studio的帮助下,我尝试获取有关基线等任务的信息。我可以阅读并获取所有项目,但无法获取任务列表。我怎样才能得到它?这是我的代码示例:
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);
}
}
}
之后我有以下错误:
"Microsoft.SharePoint.Client.CollectionNotInitializedException" in Microsoft.SharePoint.Client.Runtime.dll
集合尚未初始化。它 尚未请求或请求尚未执行。它可能会 需要明确请求。
您需要先加载任务:
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);
}
}
}