0

I want to link the existing work items which are already created in a project under Azure DevOps by writing a code or program in C#, so is there any kind of API or SDK which can be used to link the work items programmatically?

The Workitems can be of any type i.e.

  • Bug
  • User Story
  • Issue
  • Task etc.

The linking between the Workitems can also be of any type i.e. Relational, Parent-Child, etc.

Recently I referred to this link for my problem.

The link contains issue very much related and similar to mine, however it is not working as expected when I tried it.

Shirin
  • 1
  • Welcome. Sadly _[questions asking us to recommend or find a book, tool, **software library**, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](http://stackoverflow.com/help/on-topic)_. Good luck! –  Jul 02 '20 at 04:32
  • _"when I tried it it is not working as expected."_ I'd suggest to rephrase the question to focus on this point. Please provide the relevant code that you tried and that is not working ([mcve]), with some information about what you mean by "not working" (error? no effect? unexpected output?) – Pac0 Jul 02 '20 at 04:37

1 Answers1

0

Given:

//int relatedId = ...
//int id = ...
//CancellationToken token = ...
//string organization = ...
//string projectName = ...
//WorkItemTrackingHttpClient azureClient = ...

Create a JsonPatchDocument as below:

JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Add(
    new JsonPatchOperation
    {
        From = null,
        Operation = Operation.Add,
        Path = "/relations/-",
        Value = new {
                        rel = "System.LinkTypes.Related",
                        url = $"https://dev.azure.com/{organization}/{projectName}/_workitems/edit/{relatedId}",
                        attributes = new
                        {
                            comment = $"Created programmatically on {DateTime.Now}."
                        }
                    }
    }
);

and call this async method of Azure DevOps SDK:

await azureClient.UpdateWorkItemAsync(patchDoc, id, false, true, true, WorkItemExpand.All, cancellationToken: token);

In the case above we created a related link using System.LinkTypes.Related referenceName. For a full link types reference guide in Azure DevOps refer to this so's question or this microsoft's doc.