1

I need to try retrieve a list of changesets that are linked to certain work items and display info on them in the program. But I'm having a lot of trouble figuring it out.

I have been searching the internet extensively for 4 days and have come up empty handed. Is doing this even possible?

I have already tried using wiql http client but it seems to only be able to return work item information.

Luke Clark
  • 13
  • 2

2 Answers2

0

Use Get Work Item with $expand=relations. In this case, you receive all relations including changesets. Example:

enter image description here

Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • so i have figured out that part just now, but my problem now is that i need to be able to filter out the relations so its just changesets and then store their ID's and dates and comments in a class list – Luke Clark Jan 13 '23 at 12:28
0

You can reference bellow sample to do that programmatically.

C# sample for your reference to retrieve a list of changesets that are linked to a specific work item.

using System;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri accountUri = new Uri("https://dev.azure.com/{organization}");                 
            String personalAccessToken = "PAT-Here";              
            int workItemId = 227;   // ID of a work item

            // Create a connection to the account
            VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));

            // Get an instance of the work item tracking client
            WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

            WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(workItemId, expand: WorkItemExpand.Relations).Result;

            Console.WriteLine(workitem.Id);

            Console.WriteLine("Relations with changesets associated:");
            foreach (var relation in workitem.Relations)
            {

                if (relation.Rel == "ArtifactLink")
                {
                    Console.WriteLine(relation.Url);
                }
            }
            Console.ReadLine();
        }
    }
}

enter image description here

PowerShell script calling the Get Work Item REST API to retrieve a list of changesets that are linked to a specific work item.

Param(
   [string]$orgurl = "https://dev.azure.com/{organization}", 
   [string]$project = "ProjectName",
   [string]$workitemid = "227",
   [string]$user = "",
   [string]$token = "PAT-Here"
)

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

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

#Get changesets
$changesets = $response.relations | where {$_.rel -eq 'ArtifactLink' -and $_.attributes.name -eq "Fixed in Changeset"}

if($changesets){
    
    $changesets | select @{N="ChangesetID";E={$_.attributes.comment}}, @{N="CreatedDat";E={$_.attributes.resourceCreatedDate}}, url
}
else {
  Write-Host "No changeset associated to work item $workitemid ."
}

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • I tried to run this code as is (of course with my login info and my work item id), and it says i have no job functions found. I'm not sure why – Luke Clark Jan 13 '23 at 13:23
  • @LukeClark Not sure what job functions you mentioned. What about running the PowerShell script? Anyway, check if this hleps : https://stackoverflow.com/questions/47682760/no-job-functions-found-try-making-your-job-classes-and-methods-public – Andy Li-MSFT Jan 16 '23 at 06:34
  • i got that error message when i ran the powershell script. i also looked at that thread and it didnt help my problem – Luke Clark Jan 16 '23 at 11:20
  • @LukeClark How did you run the PowerShell script? No jobs involved in the script. Just try to past the script and run it in Windows PowerShell ISE to see if it works. – Andy Li-MSFT Jan 16 '23 at 11:31
  • I did exactly that and it just doesnt want to work – Luke Clark Jan 16 '23 at 13:07
  • @LukeClark That doesn't make sense. Can you please share the screenshot running the script with error? – Andy Li-MSFT Jan 17 '23 at 05:52