TFS How to get links from work item id using REST

2019-06-06 05:20发布

问题:

I'm trying to get the list of links in TFS given the work item ID using a rest API. I want to filter out the links so that I only have the commits. What format for a web request would I use? I've tried

https://{server & port}/{project}/_workitems?id=140464

but to no avail - It brings me to the 'assigned' to me query area I have also been omitting API-version=4.1 from the URL because for some reason that doesn't work...

Also, I tried looking up the work item in a query but there is little helpful information about the links. I need at least the link's title, but there are only methods to see its type

Commit in Links

回答1:

You can retrieve all the relations (linked work items) for a specific workitem via below REST API:

GET http://server:8080/tfs/DefaultCollection/_apis/wit/workitems/1?$expand=all 

Then you can get the ID type title for each of the linked work items in a loop.

Just try below PowerShell sample to get the links' information with the REST API:

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
   [string]$workitemid = "1",
   [string]$user = "username",
   [string]$token = "password"
)

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

#Get workitem relateions
$baseUrl = "$collectionurl/_apis/wit/workitems/$($workitemid)?"+"$"+"expand=all"            
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$witurls = $response.relations.url 

#Retrieve the linked work items in a loop
$linkedwits = @()

foreach ($witurl in $witurls)
{
$linkedwit = Invoke-RestMethod -Uri $witurl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$customObject = new-object PSObject -property @{
          "LinkedWitID" = $linkedwit.id
          "WorkItemType" = $linkedwit.fields.'System.WorkItemType'
          "Title" = $linkedwit.fields.'System.Title'
        } 

    $linkedwits += $customObject
}

$linkedwits | Select `
                LinkedWitID,
                WorkItemType, 
                Title #|export-csv -Path C:\LC\Links.csv -NoTypeInformation