Skip to content

Commit 2f4c715

Browse files
Merge pull request #8320 from MicrosoftDocs/users/chcomley/copilot-ai-wiql-476192
Add Copilot example to WIQL syntax article
2 parents bb76f20 + 7a71aa6 commit 2f4c715

File tree

1 file changed

+97
-16
lines changed

1 file changed

+97
-16
lines changed

docs/boards/queries/wiql-syntax.md

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ description: Learn about the reference syntax for the Work Item Query Language u
55
ms.custom: boards-queries
66
ms.service: azure-devops-boards
77
ms.topic: reference
8+
ai-usage: ai-assisted
89
ms.assetid: 95DAF407-9208-473D-9F02-4B6E7F64AD0A
910
ms.author: chcomley
1011
author: chcomley
1112
monikerRange: '<= azure-devops'
12-
ms.date: 09/12/2025
13+
ms.date: 09/17/2025
1314
---
1415

1516
# Work Item Query Language (WIQL) syntax reference
@@ -62,7 +63,7 @@ ASOF '02-11-2025'
6263
| `ASOF` | Specifies a historical query by indicating a date for when the filter is to be applied. For example, this query returns all user stories that were defined as *Active* on February 11, 2025. Specify the date according to the guidance provided in [Date and time pattern](#date-and-time-pattern).<br>`ASOF '02-11-2025'` |
6364

6465
> [!NOTE]
65-
> WIQL queries made against Azure Boards must not exceed 32 K characters. The system doesn't allow you to create or run queries that exceed that length.
66+
> WIQL queries made against Azure Boards must not exceed 32-K characters. The system doesn't allow you to create or run queries that exceed that length.
6667
6768
[!INCLUDE [date-time-pattern](../includes/date-time-pattern.md)]
6869

@@ -248,7 +249,7 @@ The following table lists the macros or variables you can use within a WIQL quer
248249
| `@Me` | Use this variable to automatically search for the current user's alias in a field that contains user aliases. For example, you can find work items that you opened if you set the `Field` column to `Activated By`, the `Operator` column to `=`, and the `Value` column to `@Me`. |
249250
| `@CurrentIteration` | Use this variable to automatically filter for work items assigned to the current sprint for the selected team based on the selected team context. |
250251
| `@Project` | Use this variable to search for work items in the current project. For example, you can find all the work items in the current project if you set the `Field` column to `Team Project`, the `Operator` column to `=`, and the `Value` column to `@Project`. |
251-
| `@StartOfDay`<br/>`@StartOfWeek`<br/>`@StartOfMonth`<br/>`@StartOfYear` | Use these macros to filter `DateTime` fields based on the start of the current day, week, month, year, or an offset to one of these values. For example, you can find all items created in the last 3 months if you set the `Field` column to `Created Date`, the `Operator` column to `>=`, and the `Value` column to `@StartOfMonth - 3`. |
252+
| `@StartOfDay`<br/>`@StartOfWeek`<br/>`@StartOfMonth`<br/>`@StartOfYear` | Use these macros to filter `DateTime` fields based on the start of the current day, week, month, year, or an offset to one of these values. For example, you can find all items created in the last three months if you set the `Field` column to `Created Date`, the `Operator` column to `>=`, and the `Value` column to `@StartOfMonth - 3`. |
252253
| `@Today` | Use this variable to search for work items that relate to the current date or to an earlier date. You can also modify the `@Today` variable by subtracting days. For example, you can find all items activated in the last week if you set the `Field` column to `Activated Date`, the `Operator` column to `>=`, and the `Value` column to `@Today - 7`. |
253254
| `[Any]` | Use this variable to search for work items that relate to any value that is defined for a particular field. |
254255

@@ -265,7 +266,7 @@ WHERE
265266

266267
### `@today` macro
267268

268-
You can use the `@today` macro with any `DateTime` field. This macro replaces midnight of the current date on the local computer that runs the query. You can also specify `@today+x` or `@today-y` using integer offsets for x days after `@today` and y days before `@today`, respectively. A query that uses the `@today` macro can return different result sets depending on the time zone in which it's run.
269+
You can use the `@today` macro with any `DateTime` field. This macro replaces midnight of the current date on the local computer that runs the query. You can also specify `@today+x` or `@today-y` using integer offsets for x days after `@today` and y days before `@today`, respectively. A query that uses the `@today` macro can return different result sets depending on the time zone in which it runs.
269270

270271
The following examples assume that today is 1/3/2025.
271272

@@ -625,20 +626,100 @@ WHERE
625626
)
626627
```
627628

628-
## Related content
629+
## Use Copilot to write, fix, and optimize WIQL
629630

630-
- [Query fields, operators, values, and variables](query-operators-variables.md)
631-
- [Work item fields and attributes](../work-items/work-item-fields.md)
632-
- [About managed queries](about-managed-queries.md)
633-
- [Define a query](using-queries.md)
631+
You can use an AI assistant (for example, GitHub Copilot or other copilots) to help create, correct, or optimize WIQL queries. Treat Copilot as a productivity aid—not an authoritative source—and always review and test any generated query before running it against production data.
634632

635-
<!---
636-
https://msdn.microsoft.com/library/bb130306.aspx
637-
-->
633+
Guidance and best practices:
638634

639-
<!---
635+
- **Capabilities**: Copilot can translate plain-language requirements into WIQL, fix syntax errors (unmatched brackets, missing commas, incorrect keywords), convert SELECT lists between friendly and reference names, generate `ASOF` clauses or date literals, and suggest clause rewrites to narrow or broaden result sets.
636+
- **Validate**: Always validate generated WIQL in the Query Editor or a safe test project. Check macros (for example `@Me`, `@Today`) and locale-dependent date formats before use.
637+
- **Security**: Never paste secrets, access tokens, or any private connection strings into prompts. Remove or redact any sensitive values in examples you feed to Copilot.
638+
- **Performance**: Ask Copilot to minimize result payloads (return only needed fields), add appropriate WHERE filters, and avoid overly broad use of `IN` or unbounded `LIKE` searches. Remember the 32-K character limit for WIQL queries.
639+
- **Review edge cases**: Confirm behavior for historical (`ASOF`) queries, tree/link queries (`FROM workItemLinks`), and `WAS EVER`/`EVER` operators that scan revisions—these can be more complex and might need manual tuning.
640640

641-
> [!WARNING]
642-
> You can use a WorkItem that was returned by a query to get the value of a Field, even if the query did not return the value. If you do this, another round trip to the server occurs. For more information, see Performance Considerations.
641+
Example - Generate WIQL from plain English:
643642

644-
-->
643+
Prompt: "Return ID and Title for active Bugs assigned to @Me in project 'Fabrikam' and modified in the last 30 days. Sort by ChangedDate desc."
644+
645+
Copilot produces:
646+
647+
```WIQL
648+
SELECT [System.Id], [System.Title]
649+
FROM workitems
650+
WHERE
651+
[System.TeamProject] = 'Fabrikam'
652+
AND [System.WorkItemType] = 'Bug'
653+
AND [System.State] = 'Active'
654+
AND [System.AssignedTo] = @Me
655+
AND [System.ChangedDate] >= @Today - 30
656+
ORDER BY [System.ChangedDate] DESC
657+
```
658+
659+
### Automate WIQL queries with REST API and AI
660+
661+
You can use AI assistants, like Copilot, to automate the two-step WIQL REST API process:
662+
663+
1. Use the [Query By Wiql REST API](/rest/api/azure/devops/wit/wiql/query-by-wiql) to retrieve work item IDs matching your WIQL.
664+
2. Use the [Get Work Items API](/rest/api/azure/devops/wit/work-items/list) to fetch full details for those IDs.
665+
666+
AI can help you:
667+
- Generate WIQL from plain language, then chain the two API calls in code (for example, Python, PowerShell, or JavaScript).
668+
- Format and summarize results for dashboards or reports.
669+
670+
> [!TIP]
671+
> For more information about REST API, see [Query By Wiql (REST API)](/rest/api/azure/devops/wit/wiql/query-by-wiql?view=azure-devops-rest-6.1&tabs=HTTP&preserve-view=true).
672+
673+
**Example: Automate WIQL with Python and AI**
674+
675+
Suppose you want to list the titles and states of all active bugs in a project.
676+
677+
**Prompt to Copilot:**
678+
"Write Python code that uses the Azure DevOps REST API to list the titles and states of all active bugs in my project. Use WIQL to get the IDs, then fetch the details for those IDs."
679+
680+
You can use Copilot to generate code like this:
681+
682+
```python
683+
import requests
684+
685+
# Azure DevOps organization and project info
686+
org = "your-org"
687+
project = "your-project"
688+
pat = "your-personal-access-token"
689+
headers = {"Authorization": f"Basic {pat}"}
690+
691+
# Step 1: Run WIQL query to get work item IDs
692+
wiql = {
693+
"query": """
694+
SELECT [System.Id]
695+
FROM workitems
696+
WHERE [System.TeamProject] = '{project}'
697+
AND [System.WorkItemType] = 'Bug'
698+
AND [System.State] = 'Active'
699+
""".format(project=project)
700+
}
701+
wiql_url = f"https://dev.azure.com/{org}/{project}/_apis/wit/wiql?api-version=6.1-preview.2"
702+
resp = requests.post(wiql_url, json=wiql, headers=headers)
703+
ids = [item["id"] for item in resp.json()["workItems"]]
704+
705+
# Step 2: Get work item details
706+
if ids:
707+
ids_str = ",".join(map(str, ids))
708+
details_url = f"https://dev.azure.com/{org}/{project}/_apis/wit/workitemsbatch?api-version=6.1-preview.1"
709+
body = {"ids": ids, "fields": ["System.Title", "System.State"]}
710+
details_resp = requests.post(details_url, json=body, headers=headers)
711+
for item in details_resp.json()["value"]:
712+
print(f"{item['fields']['System.Title']} - {item['fields']['System.State']}")
713+
else:
714+
print("No active bugs found.")
715+
```
716+
717+
## Related content
718+
719+
- [Query By Wiql (REST API)](/rest/api/azure/devops/wit/wiql/query-by-wiql?view=azure-devops-rest-6.1&tabs=HTTP&preserve-view=true)
720+
- [Get Work Items (REST API)](/rest/api/azure/devops/wit/work-items/list?view=azure-devops-rest-6.1&tabs=HTTP&preserve-view=true)
721+
- [Query quick reference](query-index-quick-ref.md)
722+
- [Work item fields and attributes](../work-items/work-item-fields.md)
723+
- [Link type reference](link-type-reference.md)
724+
- [Permissions and groups](../../organizations/security/permissions.md)
725+
- [Set your preferences](../../organizations/settings/set-your-preferences.md)

0 commit comments

Comments
 (0)