Search code examples
c#azureazure-devopsazure-devops-rest-apitfs-workitem

Ping (@) user in Azure DevOps comment


I have the method below that posts a comment to a work-item in Azure DevOps.

How can i @ a user in the comment?

The Two examples below just posts a string that is @firstName lastNameand not tagging the user.

pMessage = "@User you need to take a look at this workitem"

pMessage = "@firstName lastName <mail> you need to take a look at this workitem"

public async Task PingUser(List<int> pId, string pMessage, VssConnection pConnection)
{
    WorkItemTrackingHttpClient client = pConnection.GetClient<WorkItemTrackingHttpClient>();

    foreach (var id in pId)
    {
        await client.UpdateWorkItemAsync(
            new JsonPatchDocument()
            {new JsonPatchOperation(){
                Operation = Operation.Add,
                Value = pMessage,
                Path = "/fields/System.History",
            }}, id);
    }
}

Solution

  • If you perform Get operation you can see the format you need:

    var wi = workitemClient.GetWorkItemAsync("project", id).Result;
    

    The wi.Fields["System.History"] value is:

    enter image description here

    So the format is:

    <a href="#" data-vss-mention="version:2.0,userid"></a>
    

    Replace the userid with the User Id, to get it you can use User Entitlements - List Rest API.