Search code examples
azure-devopstfs-workitemworkitem

Azure DevOps: create a comment on behalf of another user


I'm looking for a way to add a comment to a work item on behalf of another user (impersonate another user).

        VssConnection connection = new VssConnection(new Uri(url), new VssClientCredentials());
        WorkItemTrackingHttpClient client = connection.GetClient<WorkItemTrackingHttpClient>();

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/System.History",
                Value = "Sample comment 1"
            }
        );

        await client.UpdateWorkItemAsync(patchDocument, id);

Solution

  • To create a comment (or make a change on behalf of someone) on a work item in Azure DevOps you need to set System.ChangedBy field in the patch document and also use bypassRules:true

            WorkItemTrackingHttpClient client = connection.GetClient<WorkItemTrackingHttpClient>();
    
            patchDocument.Add(
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path = "/fields/System.History",
                    Value = "Sample comment 1"
                }
            );
            patchDocument.Add(
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path = "/fields/System.ChangedBy",
                    Value = "user@onbehalfof.com" //can be valid user id (guid) or user email (domain\alias for onprem).
                });
    
            await client.UpdateWorkItemAsync(patchDocument, id, bypassRules:true);
    

    Also, to be able to set bypassRules:true - the identity that performs the operation must have the appropriate permission: "Bypass rules on work item updates"