Search code examples
azure-devops

How can I create a Work Item linked to existing item using DevOps rest api?


So, I need to define Link type (Child or Parent) and ID

Can't find what to add, but stackoverflow says: It looks like your post is mostly code; please add some more details. So i am adding details

def CreateBug(Title, Description, AssignedTo, Tag, LinkTo):
url = 'https://devops.xp2131231231aare.com:81/suort/Mart/_apis/wit/workitems/$Bug?api-version=5.1'

data = [
    {
    "op": "add",
    "path": "/fields/System.Title",
    "value": Title
    },
    {
    "op": "add",
    "path": "/fields/System.IterationPath",
    "value": "Maort\\201_02"
    },
    {
    "op": "add",
    "path": "/fields/System.AssignedTo",
    "value": AssignedTo
    },
    {
    "op": "add",
    "path": "/fields/System.Tags",
    "value": Tag
    },
    {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.TCM.ReproSteps",
    "value": Description
    }
]
r = requests.post(url, json=data,
    headers={'Content-Type': 'application/json-patch+json'},
    auth=(user_name_x, token_x))

res = r.json()
#pprint(res)

id_ = res['id']
return (id_)

ScreenShot


Solution

  • You can add the following request body when you create work items. Here are my samples:

    Add a parent link:

    {
      "op": "add",
      "path": "/relations/-",
      "value": {
        "rel": "System.LinkTypes.Hierarchy-Reverse",
        "url": "https://dev.azure.com/{Organization}/_apis/wit/workItems/{ID}",// Replace with your organization name and work item ID
        "attributes": {
          "comment": "Making a new link for the dependency"
        }
      }
    }
    

    Add a child link:

    {
      "op": "add",
      "path": "/relations/-",
      "value": {
        "rel": "System.LinkTypes.Hierarchy-Forward",
        "url": "https://dev.azure.com/{Organization}/_apis/wit/workItems/{ID}",// Replace with your organization name and work item ID
        "attributes": {
          "comment": "Making a new link for the dependency"
        }
      }
    }
    

    Here are links to documentation (v7.1) which you can refer to: