Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devops-rest APi - $expand not working for "Work Items - Get Work Items Batch"

I want to access some data from selected work items.
Below is my working code.

function postApiData(ApiUrl, responseBody, token) {
            var res = '';
            $.ajax({
                type: 'POST',
                async: false,
                url: ApiUrl,
                contentType: 'application/json',
                data: JSON.stringify(responseBody),
                cache: false,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa("" + ":" + _token));
                },
            }).done(function (data) {
                res = data;
            });
        return res;
    };

 var d = {
                    "ids": itemlist,
                    "fields": ["System.Id", "System.WorkItemType", "System.Title", "System.AssignedTo", "System.State", "System.Tags", "cust.PID", "cust.Product", "cust.ReleasedToProduction"]
                };
                var itemdata = postApiData('https://dev.azure.com/COMP/products/_apis/wit/workitemsbatch?$expand=relations&api-version=5.1', d, '');

However, $expand is not working here to get relations. query gives the result and always ignores $expand.

I have also tried to pass $expand in request body, but it is also not working. Can anyone please guide here?

like image 999
Jigar Parekh Avatar asked Oct 16 '25 13:10

Jigar Parekh


1 Answers

That's because the expand parameter can not be used with the fields parameter if the expand value is relations.

You could execute this api with your request body in Postman. You will get the clearly message that why you can not apply it.

enter image description here


To use your API, if you specify the fields in the request body, then expand should not be used any more, and vice versa. This is as designed, and it has been hardcoded into our scripts. We do not allow another $expand value if it is not the None or Links.

For the 5 values of $expand(None, Relations, Fields, Links, All), only None and Links can work with fields successfully in API. This is a universal rule which apply to all APIs, including this one.


Also, please use $expand=Relations in request body instead of in URI which shown as the document described. Here if you use it in URI, it will not be used by server script since the method that this API called does not has this parameter. Another API which can use $expand in the URI (As normal, we would mention and show whether it can be used in URI in document), the corresponding scripts have parameters can access these value which passed by URI.

So, here please use $expand=Relations in request body, then you would get the result with all fields and its all relations.

like image 54
Mengdi Liang Avatar answered Oct 18 '25 08:10

Mengdi Liang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!