M.G
M.G

Reputation: 11

TFS adding comments to discussion

I'm migrating cards from LeanKit and I need to add comments to Discussion in cards on TFS. How can I add programitcally comment to WorkItem as other user? Is it possible? I found only adding comments by History property but as a logged in user.

Thanks!

Upvotes: 1

Views: 971

Answers (2)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30372

By default we can only add comments by the logged in user.

However you could add the comments to discussion with another user using the REST API to update the value of System.ChangedBy field with bypassRules enabled:

Below sample for your reference:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection",
   [string]$projectName = "ProjectName",
   [string]$workitemID = "26",
   [string]$user = "username",
   [string]$token = "token/Password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
write-host $WorkitemType

function CreateJsonBody
{

    $value = @"
[
  {
    "op": "add",
    "path": "/fields/System.History",
    "value": "Comment here"
  },
  {
    "op": "add",
    "path": "/fields/System.ChangedBy",
    "value": "[email protected]"
  }
]
"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/$($projectName)/_apis/wit/workitems/$($workitemID)?bypassRules=true&api-version=2.2"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

The "value": "[email protected]" can be valid user id (guid) or user email of another user.

Upvotes: 1

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41575

You can add comments to the discussion as another user only if you log in with his credentials:

NetworkCredential cred = new NetworkCredential("anotherUserName", "password");
TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(new Uri("serverUrl"), cred);
_tfs.EnsureAuthenticated();

After you authenticated like another user you add text to History field and you will see the text on the discussion as a logged in another user.

Upvotes: 0

Related Questions