设置项目服务器 2013 任务级别自定义字段

Setting project server 2013 task level custom fields

我正在尝试使用 CSOM 设置任务级别自定义字段的值,但我 运行 遇到了问题,因为没有现有值,因此没有自定义字段对象。

我通过转到服务器设置 -> 企业自定义字段和查找表创建了三个任务实体自定义字段。

如果我在浏览器中编辑项目时向自定义字段添加值,则这些任务存在自定义字段对象;都与我创建的原始ID相同,我可以通过代码修改它们。

如果我第一次尝试在代码中为任务设置自定义字段值,它不起作用,我得到一个 "unknown error"(更多内容见下文)。

在我的代码中,我正在尝试为任务更新这些字段:

private static DraftTask UpdateTaskCustomFields(DraftTask task, string taskDescription, string resourceId, double estRunHours, double actRunHours)
{
    if (task == null) return null;

    projectContext.Load(task.CustomFields);
    projectContext.ExecuteQuery();

    task["Custom_0000740fd5a8e41180e0005056823bd3"] = taskDescription;
    task["Custom_85e91d7fd5a8e41180e0005056823bd3"] = estRunHours;
    task["Custom_a178dc8bd5a8e41180e0005056823bd3"] = actRunHours;

    task.CustomFields.Update();

    return task;
}

我在以下块中从另一个调用此方法:

foreach (DataRow parentTask in parentTasks.Tables[0].Rows)
{
    var parentTaskName = "";
    var taskName = string.Format("{0}.{1}", parentTask["LOT_ID"], parentTask["SPLIT_ID"]);

    var taskDescription = parentTask["WO_DESCRIPTION"].ToString();
    var estRunHours = Convert.ToDouble(parentTask["RUN_HRS"]);
    var actRunHours = Convert.ToDouble(parentTask["ACT_RUN_HRS"]);
    var resourceId = "";

    var task = GetTask(tasks, taskName);

    if (task != null)
    {
        UpdateTaskCustomFields(task, taskDescription, resourceId, estRunHours, actRunHours);

        itemCount += 3;
    }

    if (itemCount++ < batch) continue;

    itemCount = 0;

    if (!UpdateDraft(projectDraft)) ExitApp();
}

在第一个任务中,它调用 UpdateTaskCustomFields 方法没有任何错误,但在第二个任务中,projectContext.ExecuteQuery() 调用引发以下错误:

Microsoft.SharePoint.Client.ServerException was unhandled
HResult=-2146233088
Message=Unknown Error
Source=Microsoft.SharePoint.Client.Runtime
ServerErrorCode=-1
ServerErrorTraceCorrelationId=b58ee69c-a7fb-40f8-19b2-171646b3a6c8
ServerErrorTypeName=Microsoft.SharePoint.Client.UnknownError
ServerStackTrace=""
StackTrace:
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at QueueCreateProject.Program.UpdateTaskCustomFields(DraftTask task, String taskDescription, String resourceId, Double estRunHours, Double actRunHours) in c:\Projects\ProjectServerApp\ProjectServerApp\Program.cs:line 617
   at QueueCreateProject.Program.ImportTasksFromVisual() in c:\Projects\ProjectServerApp\ProjectServerApp\Program.cs:line 208
   at QueueCreateProject.Program.Main(String[] args) in c:\Projects\ProjectServerApp\ProjectServerApp\Program.cs:line 41
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

我想也许我还需要先将自定义字段直接添加到任务中,但这已经给我关于该字段的错误 existing/reserved。

我假设我的整个过程是错误的,或者我需要以某种方式或类似的方式为任务实例化自定义字段,但我不知道该怎么做。

如有任何帮助,我们将不胜感激。

谢谢!

体重

Peter Holpar,MVP 解决了我的问题 here

他给了我一个例子,我用它来找出我做错了什么。

自定义字段的内部名称无效,所以我首先按名称获取自定义字段对象(来自 projectContext.CustomFields 集合,而不是任务 CustomFields 集合),然后设置值。

线程显示了我经历的过程,但这是最终的工作代码;我希望它能帮助到同一条船上的其他人。

using (projectContext = new ProjectContext(pwaPath))
{
 projectContext.Load(projectContext.Projects, ps => ps.Include(p => p.Name));
 projectContext.Load(projectContext.CustomFields);
 projectContext.ExecuteQuery();

 var proj = projectContext.Projects.FirstOrDefault(p => p.Name == projectName);
 var cfInternalName = projectContext.CustomFields.FirstOrDefault(q => q.Name == "Task Description").InternalName;

 var draftProj = proj.CheckOut();
 projectContext.Load(draftProj, p => p.Tasks.Include(t => t.Id, t => t.Name, t => t.Parent));
 projectContext.ExecuteQuery();

 var tasks = draftProj.Tasks;
 var draftTask = tasks.FirstOrDefault(q => q.Name == "1.0");
 draftTask[cfInternalName] = "My Description";

 draftProj.Publish(true);

 projectContext.ExecuteQuery();
}

体重