Azure devops 如何在Azure Devops中使用Azure Devops API在C#中创建/更新具有父子关系的工作项

Azure devops 如何在Azure Devops中使用Azure Devops API在C#中创建/更新具有父子关系的工作项,azure-devops,azure-devops-rest-api,Azure Devops,Azure Devops Rest Api,我正在尝试使用API在Azure devops中创建/更新工作项。如果项目没有任何关系,我可以创建/更新项目。但如果我指定了关系,例如父子关系,则我得到以下错误: TF401349:发生意外错误,请验证您的请求并重试 我正在使用JsonPatchDocument创建/更新工作项。示例如下: class Example { JsonPatchOperation AddRelationship(JsonPatchDocument doc, string rel, WorkItem linke

我正在尝试使用API在Azure devops中创建/更新工作项。如果项目没有任何关系,我可以创建/更新项目。但如果我指定了关系,例如父子关系,则我得到以下错误:

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

我正在使用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" } }
        };
}
也许您的代码必须是这样的:

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

添加您的示例。如果没有源代码,很难找到错误。)也许这个链接对你有用:@ShamraiAleksander请看我使用的示例。。谢谢。我已经检查了“替换”和“添加”操作。因为工作项只有一个子项,所以我尝试提供Path=“/relations/1”,它给出了超出范围的异常。但当我提供正确的索引,即“/relations/0”时,我会得到上述异常。根据此链接,我们可以通过“/relations/[index]/attributes/comment”更改属性。如果要将链接设置为其他工作项:删除旧链接并添加新链接。