如何通过VSTS REST API将工作项添加为父工作项的子项?
Alright, let's tackle this—creating a work item and linking it to a parent via the Azure DevOps (formerly VSTS) REST API is totally doable once you understand how the System.LinkTypes.Hierarchy-Reverse link type works. Here's a clear breakdown:
What is System.LinkTypes.Hierarchy-Reverse?
First, let's demystify that link type:
System.LinkTypes.Hierarchyis the "forward" link (used when linking a parent to a child from the parent's side)System.LinkTypes.Hierarchy-Reverseis the "reverse" link—this is exactly what you need when creating a new child work item and connecting it directly to an existing parent. It tells the API to establish the parent-child hierarchy from the child's perspective.
Step-by-Step Implementation
1. Use the Correct API Endpoint
You'll use the Create Work Item POST endpoint (adjust the API version to match your environment if needed):
POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${workItemType}?api-version=7.1-preview.3
Replace placeholders like {organization}, {project}, and ${workItemType} (e.g., Task, Bug) with your actual values.
2. Build the JSON Patch Request Body
The API uses JSON Patch format to define both the work item's fields and the parent link. Here's a complete, ready-to-use example:
[ { "op": "add", "path": "/fields/System.Title", "value": "New Child Task: Fix Login Button Alignment" }, { "op": "add", "path": "/relations/-", "value": { "rel": "System.LinkTypes.Hierarchy-Reverse", "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{parentWorkItemId}", "attributes": { "comment": "Linked to parent user story via API" } } }, // Add more fields here if needed { "op": "add", "path": "/fields/System.AssignedTo", "value": "Your Name" } ]
Key Notes on the Request:
- The
/relations/-path appends a new link to the work item's relations array (the-means "add to the end"). - The
relproperty must be exactlySystem.LinkTypes.Hierarchy-Reverse—this is non-negotiable for establishing the parent-child link. - Replace
{parentWorkItemId}with the numeric ID of your existing parent work item (e.g., a User Story or Epic). - You can add any other standard work item fields (like description, priority) by including additional
addoperations in the patch array.
3. Authentication
Don't forget to include valid authentication:
- Use a Personal Access Token (PAT) with the Work Items (Read & Write) scope. Encode your PAT in Base64 and add it to the request header:
Authorization: Basic {base64-encoded-PAT} - Alternatively, use an OAuth token if you're integrating with a service or app.
How It All Comes Together
When you send this request:
- The API creates the new child work item with all the fields you defined.
- It automatically establishes the hierarchy link: the parent work item will show this new item as a child in its Links section, and the child work item will have its Parent field auto-populated with the parent's details.
内容的提问来源于stack exchange,提问作者Joe

