top of page

CSOM: Working with Office365 Project Online Tasks - Part I

Updated: Mar 22, 2019

This article series focuses on working with office 365 project online plan schedule tasks using Client Side Object model approach.

Previously we have seen how the same operations works using JSOM approach. Both approaches are identical.

Creating Console App and Importing Package: 

SharePoint and Project Server online libraries are required to work with the project server data using CSOM model. The libraries can be downloaded from Nuget packages. The required libraries are packaged under one single package. i.e., Microsoft.SharePointOnline.CSOM

Create a visual studio console application. From Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution. From the browse tab, Find for “Microsoft.SharePointOnline.CSOM”. Select and install the package for the current solution.

The following picture depicts installing the required Nuget package.

Installing Nuget Packages for SharePoint & Project Online

Retrieve All Tasks: 

First let us look at retrieving all the tasks available on the project. There are two ways of retrieving tasks.

1. From the published project object – Advantage is project need not be checked-out. Since this is read only action, the tasks can be accessed this way.

  1. using (ProjectContext projContext = new ProjectContext("https://nakkeerann.sharepoint.com/sites/pwa"))

  2. {

  3. projContext.Credentials = new SharePointOnlineCredentials("nav@nakkeerann.onmicrosoft.com", password);

  4. var projects = projContext.Projects;

  5. // Specific Project ID

  6. var projectId = "3a000b01-55f2-e811-afac-00155d24c70d";

  7. var project = projects.GetById(projectId);

  8. var tasks = project.Tasks; projContext.Load(tasks);

  9. projContext.ExecuteQuery();

  10. foreach (var task in tasks)

  11. {

  12. Console.WriteLine(task.Name);

  13. }}


2. Other way from the draft project – Trying to retrieve after checking out the project. Don’t prefer this approach if you are just viewing/accessing tasks.

  1. private static void ViewTasksFromCheckedOutProject()

  2. {

  3. using (ProjectContext projContext = new ProjectContext("https://nakkeerann.sharepoint.com/sites/pwa"))

  4. {

  5. projContext.Credentials = new SharePointOnlineCredentials("nav@nakkeerann.onmicrosoft.com", password);

  6. var projects = projContext.Projects;

  7. var projectId = "3a000b01-55f2-e811-afac-00155d24c70d";

  8. var project = projects.GetById(projectId);

  9. // If the project is already checked out, comment the below line

  10. var draftProject = project.CheckOut();

  11. // If not already checked out, uncomment the below line

  12. //var draftProject = project.Draft;

  13. var tasks = draftProject.Tasks;

  14. projContext.Load(draftProject);

  15. projContext.Load(tasks);

  16. projContext.ExecuteQuery();

  17. foreach (var task in tasks)

  18. {

  19. Console.WriteLine(task.Name);

  20. }

  21. // Check-In if required

  22. }}

The following code snippet shows retrieving tasks, but with minimal load. It gets only the properties mentioned in the load query.

  1. private static void ViewTasksMinimal()

  2. {

  3. using (ProjectContext projContext = new ProjectContext("https://nakkeerann.sharepoint.com/sites/pwa"))

  4. {

  5. projContext.Credentials = new SharePointOnlineCredentials("nav@nakkeerann.onmicrosoft.com", password);

  6. var projects = projContext.Projects;

  7. var projectId = "3a000b01-55f2-e811-afac-00155d24c70d";

  8. var project = projects.GetById(projectId);

  9. var tasks = project.Tasks;

  10. // Mention the required properties for access. Only these properties will be retrieved on execution projContext.Load(tasks, properties => properties.Include(property => property["Name"], property => property["Custom_0652e0806606e911afb600155d48510a"]));

  11. projContext.ExecuteQuery();

  12. foreach (var task in tasks)

  13. {

  14. Console.WriteLine(task.Name); Console.WriteLine(task["Custom_0652e0806606e911afb600155d48510a"]);

  15. }

  16. }}


Retrieve Task: 

Let's see, how a particular task can be retrieved. Task ID can be used to retrieve one particular task. And necessary properties can be accessed after retrieval. The following piece of code shows, retrieving a task by project ID and task ID. And it also shows the properties (including custom) accessed.

  1. private static void ViewTaskById()

  2. {

  3. using (ProjectContext projContext = new ProjectContext("https://nakkeerann.sharepoint.com/sites/pwa"))

  4. {

  5. projContext.Credentials = new SharePointOnlineCredentials("nav@nakkeerann.onmicrosoft.com", password);

  6. var projects = projContext.Projects;

  7. var projectId = "3a000b01-55f2-e811-afac-00155d24c70d";

  8. var project = projects.GetById(projectId);

  9. var tasks = project.Tasks;

  10. var taskId = "4709f408-55f2-e811-afa6-a01d710bba4a";

  11. // Gets particular Task

  12. var task = tasks.GetById(taskId);

  13. projContext.Load(task);

  14. projContext.ExecuteQuery();

  15. Console.WriteLine(task.Name); Console.WriteLine(task["Custom_0652e0806606e911afb600155d48510a"]);

  16. }}

In the next article, let us look at more other operations for MSP project online tasks.

0 comments

Comments


bottom of page