Search code examples
azure-devopsazure-devops-rest-apiazure-devops-extensions

Getting parent of work item through REST API


We are developing an Azure DevOps extension to push changes to work items to an external system.

We would like to maintain/preserve the hierarchy in Azure DevOps (Epic-> Feature -> PBI/Bug) in the target system, so we need to figure out which parent a work item has.

When pulling the Work item entity from the API, it looks like this (abbreviated a bit)

{
    "id": 5202,
    "rev": 2,
    "fields": {
        "System.WorkItemType": "Task",
        "System.State": "To Do",
        "System.Reason": "New task",
        "System.CreatedDate": "2017-10-30T10:18:06.233Z",
        "System.CreatedBy": "Jesper Lund Stocholm",
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Scheduling.RemainingWork": 23.0,
        "Microsoft.VSTS.Common.StateChangeDate": "2017-10-30T10:18:06.233Z",
    },
    "_links": {
            "self": {
            "href": "https://{myorg}.visualstudio.com/_apis/wit/workItems/5202"
        },
        "workItemUpdates": {
            "href": "https://{myorg}.visualstudio.com/_apis/wit/workItems/5202/updates"
        },
        "workItemRevisions": {
            "href": "https://{myorg}.visualstudio.com/_apis/wit/workItems/5202/revisions"
        },
        "workItemHistory": {
            "href": "https://{myorg}.visualstudio.com/_apis/wit/workItems/5202/history"
        },
        "html": {
            "href": "https://{myorg}.visualstudio.com/web/wi.aspx?pcguid=e5d991b2-9879-497c-85fb-c618f144a9c5&id=5202"
        },
        "workItemType": {
            "href": "https://{myorg}.visualstudio.com/6847ebed-cbca-4510-8baa-228c7c55ba8d/_apis/wit/workItemTypes/Task"
        },
        "fields": {
            "href": "https://{myorg}.visualstudio.com/_apis/wit/fields"
        }
    },
    "url": "https://{myorg}.visualstudio.com/_apis/wit/workItems/5202"
}

The obvious place to look is here https://{myorg}.visualstudio.com/_apis/wit/fields

But we cannot find any traces of references to "parent entity".

Can it be true that this value is not exposed?


Solution

  • You can get all the work items links (Parent, Childs etc.) by adding the $expand=relations to the api string.

    For example:

    https://shaykia.visualstudio.com/_apis/wit/workItems/4?$expand=relations
    

    In the result you will see "relations" section:

    "relations": [
        {
          "rel": "System.LinkTypes.Hierarchy-Forward",
          "url": "http:/shaykia.visualstudio.com/_apis/wit/workItems/11",
          "attributes": {
            "isLocked": false
          }
        },
        {
          "rel": "System.LinkTypes.Hierarchy-Reverse",
          "url": "http://shaykia.visualstudio.com/_apis/wit/workItems/3",
          "attributes": {
            "isLocked": false
          }
        }
      ], 
    

    System.LinkTypes.Hierarchy-Reverse is for for parent (in this case, the work item with id 3 he is the parent), and System.LinkTypes.Hierarchy-Forward is for child.