3
votes

I am trying to create/update work item in Azure devops using API. I am able to create/update with item if it does not have any relation. But if I specify relation e.g. parent-child then I am getting below error:

TF401349: An unexpected error has occurred, please verify your request and try again.

I am using JsonPatchDocument to create/update work item. Example below:

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;

    }
}

}

Thank you.

1
Add your example. Its hard to find a mistake without source code )) maybe this link will be useful for you: azure devops rest api add and edit work item links - Shamrai Aleksander
@ShamraiAleksander please see an example I used.. Thank you. - user2663330

1 Answers

4
votes

I can not see definition for "rel" in your example. Something like this:

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

Maybe your code has to be like this:

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