Reading and Saving Projects in Project Server or Project Online
Aspose.Tasks for .NET supports creating/reading/updating/ of projects in Project Server on-premises instance or Project Online (cloud version of Project Server) account using PWA (Project Web Access) API.
Managing Projects with Project Server
Aspose.Tasks for .NET provides the ProjectServerManager class to create, read and update projects in Project Server’s instance or in Project Online account. The ProjectServerCredentials class shall be used to provide credentials that are used to connect to Project Online. Previously, Microsoft.SharePoint.Client.Runtime assembly was used to retrieve AuthToken but now Aspose.Tasks for .NET provides an option to specify SiteUrl, username, and password in order to create a connection to Project Online.
Please note that currently Aspose.Tasks for .NET cannot be used with modern authentication to Project Online. Thus legacy authentication should be enabled in your Azure portal and Office 365 Admin center.
Reading Project from Project Server
The following code example demonstrates how to read project data from Project Server.
1const string SharepointDomainAddress = "https://contoso.sharepoint.com";
2const string UserName = "admin@contoso.onmicrosoft.com";
3const string Password = "MyPassword";
4
5ProjectServerCredentials credentials = new ProjectServerCredentials(SharepointDomainAddress, UserName, Password);
6ProjectServerManager manager = new ProjectServerManager(credentials);
7IEnumerable<ProjectInfo> list = manager.GetProjectList();
8
9foreach (var info in list)
10{
11 Project project = manager.GetProject(info.Id);
12 Console.WriteLine("{0} - {1} - {2}", info.Name, info.CreatedDate, info.LastSavedDate);
13 Console.WriteLine("Resources count: {0}", project.Resources.Count);
14
15 // an user can read the project as raw data stream
16 var stream = manager.GetProjectRawData(info.Id);
17
18 // work with raw project data
19}
Creating a Project in Project Server
The following code example demonstrates how to create a project in Project Server.
1try
2{
3 const string SharepointDomainAddress = "https://contoso.sharepoint.com";
4 const string UserName = "admin@contoso.onmicrosoft.com";
5 const string Password = "MyPassword";
6
7 ProjectServerCredentials credentials = new ProjectServerCredentials(SharepointDomainAddress, UserName, Password);
8
9 Project project = new Project("New Project.mpp");
10
11 ProjectServerManager manager = new ProjectServerManager(credentials);
12 manager.CreateNewProject(project);
13}
14catch (ProjectOnlineException ex)
15{
16 Console.WriteLine(ex.Message);
17}
The following code example demonstrates how to create a project with predefined save options on Microsoft Project Online.
1Project project = new Project("New Project.mpp");
2ProjectServerCredentials credentials = new ProjectServerCredentials("https://contoso.sharepoint.com", "admin@contoso.onmicrosoft.com", "MyPassword");
3ProjectServerManager manager = new ProjectServerManager(credentials);
4ProjectServerSaveOptions options = new ProjectServerSaveOptions
5{
6 Timeout = TimeSpan.FromSeconds(10)
7};
8manager.CreateNewProject(project, options);
Updating a Project in Project Server
The following code example demonstrates how to update existing project in Project Server.
1const string URL = "https://contoso.sharepoint.com";
2const string Domain = "CONTOSO.COM";
3const string UserName = "Administrator";
4const string Password = "MyPassword";
5
6NetworkCredential windowsCredentials = new NetworkCredential(UserName, Password, Domain);
7ProjectServerCredentials projectServerCredentials = new ProjectServerCredentials(URL, windowsCredentials);
8try
9{
10 ProjectServerManager manager = new ProjectServerManager(projectServerCredentials);
11
12 var projectInfo = manager.GetProjectList().FirstOrDefault(p => p.Name == "My project");
13 if (projectInfo == null)
14 {
15 Console.WriteLine("Project 'My project' not found in working store of Project Online account.");
16 return;
17 }
18
19 Project project = manager.GetProject(projectInfo.Id);
20 project.Set(Prj.FinishDate, new DateTime(2020, 03, 01));
21
22 Task task = project.RootTask.Children.Add("New task");
23 task.Set(Tsk.Start, new DateTime(2020, 02, 26));
24 task.Set(Tsk.Duration, project.GetDuration(2, TimeUnitType.Day));
25
26 manager.UpdateProject(project);
27}
28catch (ProjectOnlineException ex)
29{
30 Console.WriteLine("Failed to update the project. Error: " + ex);
31}
The following code example demonstrates how to update a project in Project Server with save options.
1const string SharepointDomainAddress = "https://contoso.sharepoint.com";
2const string UserName = "admin@contoso.onmicrosoft.com";
3const string Password = "MyPassword";
4
5ProjectServerCredentials credentials = new ProjectServerCredentials(SharepointDomainAddress, UserName, Password);
6
7try
8{
9 ProjectServerManager manager = new ProjectServerManager(credentials);
10
11 var projectInfo = manager.GetProjectList().FirstOrDefault(p => p.Name == "My project");
12 if (projectInfo == null)
13 {
14 Console.WriteLine("Project 'My project' not found in working store of Project Online account.");
15 return;
16 }
17
18 Project project = manager.GetProject(projectInfo.Id);
19 project.Set(Prj.FinishDate, new DateTime(2020, 03, 01));
20
21 Task task = project.RootTask.Children.Add("New task");
22 task.Set(Tsk.Start, new DateTime(2020, 02, 26));
23 task.Set(Tsk.Duration, project.GetDuration(2, TimeUnitType.Day));
24
25 ProjectServerSaveOptions options = new ProjectServerSaveOptions { Timeout = TimeSpan.FromMinutes(5) };
26
27 manager.UpdateProject(project, options);
28}
29catch (ProjectOnlineException ex)
30{
31 Console.WriteLine("Failed to update the project. Error: " + ex);
32}