如何使用 Azure Devops API 在 C# 中使用 Azure Devops create/update 具有父子关系的工作项

How to create/update work item with parent-child relation in C# using Azure Devops API in Azure Devops

我正在尝试使用 API 在 Azure devops 中 create/update 工作项。如果项目没有任何关系,我可以 create/update 。但是,如果我指定关系,例如亲子然后我得到以下错误:

TF401349: 发生意外错误,请验证您的请求并重试。

我在 create/update 工作项中使用 JsonPatchDocument。示例如下:

class Example
{
    JsonPatchOperation AddRelationship(JsonPatchDocument doc, string rel, WorkItem linkedItem, bool isNew, int index)
    {
        //update link
        if (!isNew)
        {
            return new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/" + index,
                Value = new { rel, url = linkedItem.Url, attributes = new { comment = "comment while update" } }
            };
        }
        else
            return new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/-",
                Value = new { rel, url = linkedItem.Url, attributes = new { comment = "Comment while creating item" } }
            };
    }

    void Save()
    {
        // some code
        doc.Add(AddRelationship(doc, "System.LinkTypes.Hierarchy-Forward", item, isNew, index++));

        var workItem = isNew
                     ? witClient.CreateWorkItemAsync(doc, Vsts.Project, issueType, bypassRules: true, validateOnly: Mode == ProcessingMode.ReadOnly).Result
                     : witClient.UpdateWorkItemAsync(doc, existingWorkItemId.Value, bypassRules: true, validateOnly: Mode == ProcessingMode.ReadOnly).Result;

    }
}

}

谢谢。

我在你的例子中看不到 "rel" 的定义。像这样:

patchDocument.Add(new JsonPatchOperation()
{
    Operation = Operation.Add,
    Path = "/relations/-",
    Value = new {
        rel = "System.LinkTypes.Hierarchy-Forward",
        url = RelUrl,
        attributes = new
        {
            comment = "Comment for the link"
        }
    }
});

也许你的代码必须是这样的:

JsonPatchOperation AddRelationship(JsonPatchDocument doc, string relname, WorkItem linkedItem, bool isNew, int index)
{
    //update link
    if (!isNew)
    {
        return new JsonPatchOperation()
        {
            Operation = Operation.Replace,
            Path = "/relations/" + index + "/attributes/comment",
            Value = "comment while update" 
        };
    }
    else
        return new JsonPatchOperation()
        {
            Operation = Operation.Add,
            Path = "/relations/-",
            Value = new { rel = relname, url = linkedItem.Url, attributes = new { comment = "Comment while creating item" } }
        };
}