1

我需要使用 C# 和 CSOM 获取 PublishedTask 的 Parent.Id。有谁知道怎么做?

(客户端对象模型,Project Server Online,在 SharePoint 上运行。)

以下代码不起作用:

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

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

像这样更改 Load() 方法也不起作用:

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))

在所有这些情况下,PublishedTask.Parent 是未定义的。PublishedTask.Parent.Id 也未定义。在任何这些情况下尝试访问这些属性中的任何一个时都会引发错误。

有谁知道如何获得 PublishedTask.Parent.Id?

4

1 回答 1

1

解决方案是知道 Task.Parent.Id 何时为空,何时有值。下面的代码说明了这一点。

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;
    }
}

我在这里找到了这个功能:http: //chrisdomino.com/Blog/Post/An-Investigation-Into-Nullability-In-The-SharePoint-Client-Object-Model

所以下面的代码和上面的函数一起工作。

                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);
                }
于 2015-10-12T17:14:29.043 回答