C# ASP.NET 中的 Project Server 和 Project Online

Project Online 和 Project Server 是 Microsoft 提供的功能豐富的項目和項目組合管理解決方案。這兩種解決方案都提供了幾乎相似的用於創建和管理項目的功能,但是,它們的區別在於哪種解決方案最適合您的要求。例如,如果您希望本地解決方案能夠更好地控制硬件和軟件,那麼您可以選擇 Project Server。另一方面,Project Online 可用作 Project Server 的基於雲的實例,而無需您自己的基礎結構。

有時可能需要從您的應用程序中訪問來自 Project Server/Online 的項目信息。為了處理此類情況,在本文中,我將向您展示如何在 .NET 應用程序(ASP.NET 等)或 Web 服務中使用 C# 在 Project Server 或 Project Online 上創建/讀取項目.

用於 Project Server/Online 的 C# API

為了創建新項目和從 Project Server/Online 讀取現有項目,我們將使用 Aspose.Tasks for .NET。它是一個跨平台類庫,用於在 .NET Standard 或 .NET Core 應用程序中以編程方式讀取和寫入 MS Project 文件。該 API 託管在 NuGet 上,並在 下載 部分以 MSI 包和壓縮 DLL 的形式提供。

使用項目服務器

在本節中,我將向您展示如何連接到項目服務器並讀取或創建項目。為了連接到 Project Server,您需要了解以下詳細信息:

在 C# 中從 Project Server 讀取項目

以下是連接到 Project Server 並從中檢索項目列表的步驟。

下面的代碼示例演示如何在 C# 中從 Project Server 讀取項目。

string url = "https://contoso.sharepoint.com";
string domain = "CONTOSO.COM";
string userName = "Administrator";
string password = "MyPassword";

NetworkCredential windowsCredentials = new NetworkCredential(userName, password, domain);
ProjectServerCredentials projectServerCredentials = new ProjectServerCredentials(url, windowsCredentials);
ProjectServerManager manager = new ProjectServerManager(projectServerCredentials);

var list = manager.GetProjectList();
foreach (var projectInfo in list)
{
    Console.WriteLine("{0} - {1} - {2}", projectInfo.Id, projectInfo.CreatedDate, projectInfo.Name);
}

使用 C# 在 Project Server 上創建項目

在 Project Server 上創建項目非常簡單。只需按照前面示例中的相同方式連接到 Project Server,使用 Project 類加載項目文件 (.mpp) 並調用 ProjectServerManager.CreateNewProject() 方法。

以下代碼示例演示如何使用 C# 在 Project Server 上創建新項目。

Project project = new Project("New Project.mpp");
NetworkCredential windowsCredentials = new NetworkCredential("Administrator", "MyPassword", "CONTOSO.COM");
ProjectServerCredentials projectServerCredentials = new ProjectServerCredentials("https://contoso.sharepoint.com", windowsCredentials);
ProjectServerManager manager = new ProjectServerManager(projectServerCredentials);
manager.CreateNewProject(project);

使用 Project Online

使用 Project Online 與使用 Project Server 非常相似,只是在創建連接方面有所不同。為了連接到 Project Online,您需要了解以下詳細信息:

  • 網址
  • 用戶名
  • 密碼

在 C# 中從 Project Online 讀取項目

以下是從 Project Online 讀取項目的步驟:

以下代碼示例演示如何從 Project Online 中檢索項目列表。

const string SharepointDomainAddress = "https://contoso.sharepoint.com";
const string UserName = "admin@contoso.onmicrosoft.com";
const string Password = "MyPassword";

ProjectServerCredentials credentials = new ProjectServerCredentials(SharepointDomainAddress, UserName, Password);
ProjectServerManager manager = new ProjectServerManager(credentials);
IEnumerable<ProjectInfo> list = manager.GetProjectList();

foreach (var info in list)
{
    Project project = manager.GetProject(info.Id);
    Console.WriteLine("{0} - {1} - {2}", info.Name, info.CreatedDate, info.LastSavedDate);
    Console.WriteLine("Resources count: {0}", project.Resources.Count);

    // 用戶可以將項目作為原始數據流讀取
    var stream = manager.GetProjectRawData(info.Id);

    // 使用原始項目數據
}

使用 C# 在 Project Online 上創建項目

以下是在 Project Online 上創建新項目的步驟:

以下代碼示例演示如何使用 C# 在 Project Online 上創建新項目。

string sharepointDomainAddress = "https://contoso.sharepoint.com";
string userName = "admin@contoso.onmicrosoft.com";
string password = "MyPassword";

ProjectServerCredentials credentials = new ProjectServerCredentials(sharepointDomainAddress, userName, password);

Project project = new Project("New Project.mpp");

ProjectServerManager manager = new ProjectServerManager(credentials);
manager.CreateNewProject(project);

了解有關 Aspose.Tasks for .NET 的更多信息

您可以從 文檔 了解更多關於使用 Aspose.Tasks for .NET 進行項目管理的信息。