1

I need to get the Parent.Id of a PublishedTask using C# and CSOM. Does anyone know how?

(Client-side Object Model, Project Server Online, running on SharePoint.)

The following code does not work:

ProjContext.Load(PublishedProject.Tasks);
ProjContext.ExecuteQuery();

foreach (PublishedTask Task in PublishedProject.Tasks)
{    
    Console.WriteLine(Task.Parent.Id);
}

Altering the Load() method like this also doesn't work:

ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Parent))
ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent))
ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent, pt => pt.Parent.Id))

In all these cases, the PublishedTask.Parent is undefined. PublishedTask.Parent.Id is also undefined. An error is thrown when attempting to access any of these properties under any of these scenarios.

Does anyone know how to get the PublishedTask.Parent.Id?

Ray White
  • 297
  • 4
  • 19

1 Answers1

1

The solution is to know when Task.Parent.Id is null, and when it has a value. The code below tells that.

public static bool IsNull(ClientObject clientObject)
{
    //check object
    if (clientObject == null)
    {
        //client object is null, so yes, we're null (we can't even check the server object null property)
        return true;
    }
    else if (!clientObject.ServerObjectIsNull.HasValue)
    {
        //server object null property is itself null, so no, we're not null
        return false;
    }
    else
    {
        //server object null check has a value, so that determines if we're null
        return clientObject.ServerObjectIsNull.Value;
    }
}

I found this function here: http://chrisdomino.com/Blog/Post/An-Investigation-Into-Nullability-In-The-SharePoint-Client-Object-Model

So the code below works, with the function above.

                ProjContext.Load(PublishedProject.Tasks);
                ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent));
                ProjContext.ExecuteQuery();

                foreach (PublishedTask Task in PublishedProject.Tasks)
                {
                    string sParentId = null;
                    string sParentName = null;
                    if (!IsNull(Task.Parent))
                    {
                        sParentId = Task.Parent.Id.ToString();
                        sParentName = Task.Parent.Name;
                        string sMyName = Task.Name;
                    }
                    Console.WriteLine("{0}, {1}, {2}, {3}", Task.Name, sParentId, sParentName, Task.Work);
                }
Ray White
  • 297
  • 4
  • 19