SharePoint & Project Server

Read Task level data using CSOM

Hello Readers!

In this blog, I’m gonna discuss about reading tasks information and storing into a data Table using CSOM. This task information includes, normal text, lookup, date , Boolean type of custom fields.

Task fields such as Name, cf_UniqueIdValue, IsActive, TaskParent, PercentComplete, BaselineStart, BaselineFinish, ActualFinish, IsMilestone, Start, Finish, IsSummary and OutlineLevel etc..

**In the below code, have not only read the task value but also shown how to handle the exception if there is not value for a task.

Below is the piece of code that reads and stores in Data Table.

*** Please note that in the below code,  cf_UniqueIdValue is a lookup type task custom field not the task internal ID.

var projColl = pc.LoadQuery(pc.Projects
.Where(p => p.Name == currentProjectName) // pass the current project name here
.Include(
p => p.Id,
p => p.Name,
p => p.EnterpriseProjectType
));
pc.ExecuteQuery();
PublishedProject theProj = projColl.First();
templateName = theProj.EnterpriseProjectType.Name;

DraftProject draftProject = theProj.Draft;

pc.Load(draftProject,
p => p.Id,
p => p.Name,
p => p.Tasks,
p => p.Tasks.Include(
t => t.Id,
t => t.IsMarked,
t => t.Parent,
t => t.Name,
t => t.CustomFields,
t => t.IsActive
)
);

pc.ExecuteQuery();

//create a DataTable to store Project Tasks data.
var dt_SourceTasks = new DataTable();
dt_SourceTasks.Columns.Add("TaskName");
dt_SourceTasks.Columns.Add("UniqueId");
dt_SourceTasks.Columns.Add("IsActive");
dt_SourceTasks.Columns.Add("ParentName");
dt_SourceTasks.Columns.Add("%Complete");
dt_SourceTasks.Columns.Add("BaselineStart");
dt_SourceTasks.Columns.Add("BaselineFinish");
dt_SourceTasks.Columns.Add("ActualFinish");
dt_SourceTasks.Columns.Add("IsMilestone");
dt_SourceTasks.Columns.Add("Start");
dt_SourceTasks.Columns.Add("Finish");
dt_SourceTasks.Columns.Add("IsSummary");
dt_SourceTasks.Columns.Add("OutlineNumber");

foreach (var tsk in draftProject.Tasks)
{

try
{
TaskParent = tsk.Parent.Name;
}
catch (System.Exception ex)
{
TaskParent = "";
}

try
{
string cf_UniqueIdLookupEntry = ((string[])tsk[cf_UniqueId]).FirstOrDefault();// you can also use ((string[])tsk[cf_UniqueId]).[0];
pc.Load(tsk,
t => t.CustomFields,
t => t.CustomFields.Where(c => c.Name == "Unique Identifier"),
t => t.CustomFields.IncludeWithDefaultProperties(lkp => lkp.LookupEntries,
lkp => lkp.LookupEntries.Where(l => l.InternalName == cf_UniqueIdLookupEntry)
)

);
pc.ExecuteQuery();
try
{
cf_UniqueIdValue = tsk.CustomFields.FirstOrDefault().LookupEntries.FirstOrDefault().FullValue;
}
catch (System.Exception ex)
{
cf_UniqueIdValue = "";
}
}
catch (System.Exception ex)
{
cf_UniqueIdValue = "";
}
dt_SourceTasks.Rows.Add(new object[] { tsk.Name, cf_UniqueIdValue, tsk.IsActive,TaskParent,tsk.PercentComplete,
tsk.BaselineStart.ToShortDateString(),tsk.BaselineFinish.ToShortDateString(),
tsk.ActualFinish.ToShortDateString(),tsk.IsMilestone,tsk.Start.ToShortDateString(),
tsk.Finish.ToShortDateString(),tsk.IsSummary,tsk.OutlineLevel
}
);

}