Azure DevOps API Combine OData Queries? - azure-devops

I'm using the following 3 queries in order to get a list of Task/User Story/Feature relationship in a given Sprint (Iteration)
I'm no Odata expert, but it would seem in some way I could combine the 3 queries into 1 to retrieve Task, Parent (User Story) Parent (Feature) and associated Assigned Usernames.
Is this possible or could this be done another way?
Get Task and parent User Stories
https://analytics.dev.azure.com/{org}/{projectName}/_odata/v3.0-preview/WorkItems?$select=AssignedToUserSK,WorkItemId,Title,State,WorkItemType,ParentWorkItemId,RemainingWork,CompletedWork,OriginalEstimate&$expand=Parent($select=WorkItemId,Title,State,WorkItemType,ParentWorkItemId,StoryPoints,StackRank)&$filter=Iteration/IterationPath eq '{sprint}' and WorkItemType eq 'Task'";
Get parent Feature of User Stories (from above)
https://analytics.dev.azure.com/{org}/{projectName}/_odata/v3.0-preview/WorkItems?$select=WorkItemId, Title, State, &$filter=WorkItemId in ({parentStoryFeatureIdsStr})
Get Work Item and Assigned User Name
https://analytics.dev.azure.com/{org}/{projectName}/_odata/v3.0-preview/WorkItems?$select=WorkItemId,&$expand=AssignedTo($select=UserName)&$filter=Iteration/IterationPath eq '{sprint}' and WorkItemType eq 'Task'"

You can have a try using $expand on Descendants to query Feature and its decendants work items(User Story and Task). See below example:
https://analytics.dev.azure.com/{org}/{projectName}/_odata/v3.0-preview/WorkItems?$filter=WorkItemType eq 'Feature'&$expand=Descendants($filter=Iteration/IterationPath eq '{sprint}'; $select=AssignedToUserSK,WorkItemId,Title,State,WorkItemType,ParentWorkItemId,RemainingWork,CompletedWork,OriginalEstimate;$expand=AssignedTo($select=UserName))
Using $expand on Descendants will return all the children User Stories and grandchildren Tasks of the Feature work items.
If you have to use $expand on Parent to query from Tasks to Parent User Stories and then to Parent Feature. I am afraid it is impossible to combine the 3 queries into 1. For the maximum depth allowed for $expand on Parent is 1 .
However, You can combine the 3 queries into 2 like below(combine the 3rd query into the 1st query):
https://analytics.dev.azure.com/{org}/{projectName}/_odata/v3.0-preview/WorkItems?$select=AssignedToUserSK,WorkItemId,Title,State,WorkItemType,ParentWorkItemId,RemainingWork,CompletedWork,OriginalEstimate&$expand=Parent($select=WorkItemId,Title,State,WorkItemType,ParentWorkItemId,StoryPoints,StackRank),AssignedTo($select=UserName)&$filter=Iteration/IterationPath eq '{sprint}' and WorkItemType eq 'Task'"

Related

Create query in Azure Devops server filtering on User Stories or tasks

I'm struggling to create a query to filter on either the parent user story or the nested child task but I can't figure out if this is possible.
For example:
There is a user story assigned to user A with tasks assigned to User B and C
There is another user story assigned to user C with tasks assigned to user A, B and C.
Is there a way to create a query which returns both of the user stories and all of their tasks when filtering on user A? I also like the hierarchy to be right, ie the user stories should contain tasks as links.
As a workaround, check the pic below:
Note: If the task assigned to user A, the parent-child relationship that returns the result is the opposite
Update1
We cannot get the result in the one query, we need to create different queries to get results.
Task assign to A
User Story assign to A.
Update2
Install the extension Query Based Boards->create query->save the query and click the tab Show as Taskboard, then we could see the result.

OData Query to Only Return State Changes from Azure Devops

I have an OData Query that I am using to pull data into PowerBI that I am trying to make more efficient. I am doing a report from Azure DevOps and pulling data in from the WorkItemRevisions resource. Currently, I am pulling all the data for a Work Item and then filtering in PowerBI to only get when the State has changed. I would like to move this filtering to the Odata query so that I can minimize the data that I am pulling into the report.
Currently, I have a query like the following (simplified example used for this question)
https://analytics.dev.azure.com/{Organization}/{Project}/_odata/v3.0-preview/WorkItemRevisions?
$select=Revision,WorkItemId,WorkItemType,Title,State,ChangedDate,LeadTimeDays,ParentWorkItemId
How can this be updated so that only Revisions where the State has changed (from New to Active, Active to Done, etc) are returned?
How can this be updated so that only Revisions where the State has changed (from New to Active, Active to Done, etc) are returned?
I am afraid that OData Query could not perfectly achieve what we need.
There is a feature Revisions/any(r:r/state eq '{state}') to filter the work item has a set state in the past.
For example:
https://analytics.dev.azure.com/<Organization>/<Project>/_odata/v2.0//WorkItems?
$filter=State eq 'Closed' and Revisions/any(r:r/State eq 'Active')
This query is similar to a Work Item query that uses the Was Ever operator.
As I said, this may not be a perfect solution. That because it can only filter whether the work item has ever had a specified states, but cannot accurately determine the states of the work item must be from New to Active, Active to Done. If we change the state of the workitem from Active to Resolved, then change it from Resolved to Closed. Then this work item will appear in the query results.
In addition, even if you use the UI query, we cannot accurately query the result of the work item status changing from A to B. To achieve this goal, we need to use REST API.
So, we could use the feature Revisions/any(r:r/state eq '{state}') to reduce the data pulled into the report to a certain extent.
You can use below query to achieve what you want.
/_odata/v4.0-preview/WorkItemRevisions?&$apply=filter(((WorkItem/WorkItemType
eq 'Bug' or WorkItem/WorkItemType eq 'Product Backlog Item') and
((WorkItem/ChangedDate ge 2022-10-05Z and WorkItem/ChangedDate le
2022-11-04Z) or (WorkItem/CreatedDate ge 2022-10-05Z and
WorkItem/CreatedDate le 2022-11-04Z))))/groupby((WorkItemId,State),
aggregate(ChangedDate with min as MinChangedDate))
To filter the group by data you need to encapsulate it under $apply as it is shown above.
Above URL will return all states and their changed dates for Bug and PBI Work Item types which are added or updated with a given date range.
Hope it helps!
if you are able to use Analytics Views instead of OData, there is a dedicated field available in Analytics Views Fields setting called "State Changed Date"

Microsoft Graph query parameter

I use the below query to list all Microsoft 365 groups from our tenant. I would like to add one more filter so that it should only return the groups which have a guest member (Usertype eq 'Guest') in it. Could someone please help
https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(c:c+eq+'Unified')
Unfortunately there is no such filter to do what you are proposing, you will have to get a list of groups, then use that to query the List members endpoint /groups/{id}/members https://learn.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=http . then make your own list.
so to summarize, first run your query, get the guids for each group, then grab the members from each group and do some filtering on the client side for usertype.
In theory group/members filtering is possible as of a few weeks ago, with a query like this: https://graph.microsoft.com/beta/groups/{id}/members?$count=true&$filter=UserType eq 'Guest'
https://microsoftgraph.uservoice.com/forums/920506-microsoft-graph-feature-requests/suggestions/35630488-enable-filter-on-group-members#%7Btoggle_previous_statuses%7D
note that you need to put in a custom request header of consistencylevel as per the docs.

odata query $filter on nested navigation property

I have a Entity Customer containing Orders Navigation Property and Order containing Products Navigation Property(One-Many).
Now how i can filter all customers have ordered a Specific Product. I have tried every permutation as mentioned below but it is throwing odata exceptions:
trial 1) Customers?$filter=Orders/Products/any(d:d/ProductCode eq 'code1')
trial 2) Customers?$expand=Orders($expand=Products))&$filter=Orders/Products/any(d:d/ProductCode eq 'code1')
trial 3) Customers?$expand=Orders($expand=Products))&$filter=Orders/any(d:d/Products/ProductCode eq 'code1')
Please suggest correct format for odata query.
I think that you need two different any clauses here as I think that you are asking for "customers with any order that in turn has any product that has the product code 'code1'"
So I think that it should look like this:
Customers?$filter= Orders/any(o: o/Products/any(p: p/ProductCode eq 'code1'))
Here is an example using the example TripPin OData service: http://services.odata.org/V4/TripPinServiceRW/People?$filter=Friends/any(f:%20f/Trips/any(t:%20t/Name%20eq%20'Trip%20in%20US'))&$expand=Friends($expand=Trips)

Get TFS work item and its links using REST API

I'm using TFS REST API and am trying to retrieve work items & their child items by title (parent's title is the parameter). I can't find a way to retrieve these linked items using TFS REST API.
This is what I've tried. First I query for the work items by title:
URI = http://[tfspath]/_apis/wit/wiql?api-version=1.0
query = SELECT * FROM WorkItem WHERE [System.Title] = 'some title'
The above returns me an object WorkItems which has only the ID/URL of the matching work item. Then, I use the returned ID on the query below (lets say the id is 1234):
URI = http://[tfspath]/_apis/wit/workitems/1234?fields=System.Title&api-version=1.0
This returns the title of the item & other fields I might include on the fields list. However, I cannot find a way to include the child items in the returns. I've tried including System.RelatedLinks but this does not change the returned fields. Example:
URI = http://[tfspath]/_apis/wit/workitems/1234?fields=System.Title,System.RelatedLinkCount,System.RelatedLinks&api-version=1.0
Returns
"fields":{"System.RelatedLinkCount":4,"System.Title":"some title"}
Which means there are 4 related links to the work item "some title", but they are not being returned.
What am I missing here? How do I get these related links/child items?
Append &$expand=relations to the querystring to fetch the links collection of a workitem:
$expand enum { all, relations, none } none
Gets work item relationships (work item links, hyperlinks, file attachements, etc.).
See: https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#with-links-and-attachments
To get a work item with all details as well as the links with details, you'll need to use the APIs that are intended for reporting purposes. Due to the possible shear size of the returned document, it will be chunked and you will be given a watermark. You may need to do multiple requests.
See: https://www.visualstudio.com/en-us/docs/integrate/api/wit/reporting-work-item-links