CSOM を使用してタスク レベルのカスタム フィールドの値を設定しようとしていますが、既存の値がないためカスタム フィールド オブジェクトがない場合に問題が発生します。
[サーバー設定] -> [エンタープライズ カスタム フィールドとルックアップ テーブル] に移動して、3 つのタスク エンティティ カスタム フィールドを作成しました。
ブラウザーでプロジェクトを編集するときにカスタム フィールドに値を追加すると、それらのタスク用のカスタム フィールド オブジェクトが存在します。すべて私が作成した元のものと同じ ID で、コードを使用してそれらを変更できます。
タスクのコードで初めてカスタム フィールド値を設定しようとすると、機能せず、「不明なエラー」が発生します (詳細は後述)。
私のコードでは、タスクのこれらのフィールドを次のように更新しようとしています:
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 メソッドを呼び出しますが、2 番目のタスクでは 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:
最初にカスタム フィールドをタスクに直接追加する必要があるのではないかと考えましたが、そのフィールドが既に存在する/予約されているというエラーが表示されました。
プロセス全体が間違っているか、タスクのカスタムフィールドを何らかの方法または同様の方法でインスタンス化する必要があると想定していますが、どうすればよいかわかりません。
どんな助けでも大歓迎です。
ありがとう!
Wg