更新 VSTS 2017 中的创建者(System.CreatedBy)工作项字段

Update CreatedBy(System.CreatedBy) workitem field in VSTS 2017

我们无法使用 rest API 更新 VSTS 2017 中的 CreatedBy(System.CreatedBy) 工作项字段。用户已添加到 "Project Collection Service Accounts" VSTS 组以便在更新工作项时绕过规则。

Link: https://github.com/Microsoft/vsts-dotnet-samples/blob/master/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/WorkItemsSample.cs#L271

public WorkItem UpdateWorkItemUsingByPassRules(int id)
    {
        JsonPatchDocument patchDocument = new JsonPatchDocument();

        patchDocument.Add(
            new JsonPatchOperation() { 
                Operation = Operation.Add,
                Path = "/fields/System.CreatedBy",
                Value = "Foo <Foo@hotmail.com>"
            }
        );

        VssConnection connection = Context.Connection;
        WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

        WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id, null, true).Result;

        return result;
    }

当 validateOnly 参数设置为 true 而不是 null 时,结果输出将保留更新的创建者值。

System.CreatedBy 字段只能在创建工作项时修改。 如果工作项有多个修订版,System.CreatedBy 不能通过绕过规则进行更改.

您也可以在make an update bypassing rules中找到它:

NOTE: System.CreatedBy and System.CreatedDate can only be modified using bypass rules on work item creation, i.e. the first revision of a work item.

由于 System.CreatedBySystem.CreatedDate 记录了创建工作项的人员和时间,因此只能在您创建工作项时更新。