Navigation: Home > API Reference > Work Item Tracking API > getWorkItems Method
The getWorkItems
method retrieves multiple work items by their IDs in a single request. This method is significantly more efficient than making separate calls to retrieve individual work items, especially when retrieving many work items at once.
getWorkItems(
ids: number[],
fields?: string[],
asOf?: Date,
expand?: WorkItemExpand,
errorPolicy?: WorkItemErrorPolicy,
project?: string
): Promise<WorkItem[]>
Name | Type | Description | Required |
---|---|---|---|
ids |
number[] |
Array of work item IDs to retrieve (maximum 200) | Yes |
fields |
string[] |
Array of field names to include (if omitted, all fields are returned) | No |
asOf |
Date |
Retrieves the work items as they were at this date/time | No |
expand |
WorkItemExpand |
The expand options for work item attributes (None, Relations, Fields, Links, All) | No |
errorPolicy |
WorkItemErrorPolicy |
Controls how errors are handled (Fail, Omit) | No |
project |
string |
Project ID or name containing the work items | No |
Promise<WorkItem[]>
: A promise that resolves to an array of work item objects.
Each WorkItem
object includes:
id
: The work item IDrev
: The revision numberfields
: Dictionary of field names and valuesrelations
: Array of related work items (if expand includes Relations)_links
: Dictionary of related resources// Get multiple work items by their IDs
const workItems = await witApi.getWorkItems([42, 43, 44]);
workItems.forEach(workItem => {
console.log(`ID: ${workItem.id}, Title: ${workItem.fields["System.Title"]}`);
});
// Only retrieve specific fields for multiple work items
const fields = [
"System.Id",
"System.Title",
"System.State",
"System.AssignedTo"
];
const workItems = await witApi.getWorkItems([42, 43, 44], fields);
workItems.forEach(workItem => {
console.log(`ID: ${workItem.id}`);
console.log(`Title: ${workItem.fields["System.Title"]}`);
console.log(`State: ${workItem.fields["System.State"]}`);
console.log(`Assigned To: ${workItem.fields["System.AssignedTo"]}`);
console.log("---");
});
// Use the error policy to omit work items that aren't found
// instead of failing the entire request
const workItems = await witApi.getWorkItems(
[42, 999, 44], // Assume 999 doesn't exist
undefined,
undefined,
undefined,
1 // WorkItemErrorPolicy.Omit
);
console.log(`Retrieved ${workItems.length} work items`);
workItems.forEach(workItem => {
console.log(`ID: ${workItem.id}, Title: ${workItem.fields["System.Title"]}`);
});
// Get the work items as they were on a specific date
const asOfDate = new Date("2023-01-15");
const workItems = await witApi.getWorkItems([42, 43, 44], undefined, asOfDate);
console.log(`Work items as of ${asOfDate.toDateString()}:`);
workItems.forEach(workItem => {
console.log(`ID: ${workItem.id}, Title: ${workItem.fields["System.Title"]}`);
});
By default, if any work item ID in the request cannot be found, the entire request will fail. You can use the errorPolicy
parameter to change this behavior:
WorkItemErrorPolicy.Fail
(default): The entire request fails if any work item is not foundWorkItemErrorPolicy.Omit
: Work items that cannot be found are omitted from the resultfields
parameter for better performancegetWorkItems
instead of multiple getWorkItem
calls whenever possibleproject
parameter when you know the project, as it can improve query performanceexpand
options when you need the additional data