Search code examples
jsonresttfsazure-pipelines-build-taskwiql

VSTS / TFS Error Occurred While Trying to get Work Items With WIQL


I am using below method to obtain workitem Ids with specific title from my VSTS extension. I am using REST API , Typescript and WIQL .

public getWorkItemsbyQueryFilter(): string[] {               

            try {               

                let query =  "Select [System.Id] From WorkItems Where [System.WorkItemType] = '" + this.workItemType + "' AND [System.Title] contains 'ABC'";
                var ids : string[];           
                var self = this;
                var colURL =  this.collectionURL +this.teamProject +"/_apis/wit/wiql?api-version=1.0"

                var options = {            
                    url: colURL,
                    username: this.username,
                    password: this.password,
                    domain: this.domain,
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }, 
                    body :{
                        'query': query
                    }         
                };

                httpntlm.post(options, function(err,res) {

                    if(err) {
                        console.log("Error : +"+ err)
                        deferred.reject(err);
                    }
                    var obj = JSON.parse(res.body);

                    for (var index = 0; index < obj.workItems.length; index++) {
                       ids.push(obj.workItems[index].id);                   
                    }             

                    return ids;
                });       

            } catch (error) {
                console.log("Failed to Get Work Item Ids :"+ error);               
            }
        }

I got below error when I execute this method . As per my web research I couldn't find much to resolve this issue

Unhandled: must start with number, buffer, array or string

Next I have try this request in postman (chrome extension). I am getting new error in same. It seems like something wrong with Json ,but I couldn't figure out what exactly it is. Please be kind enough to show some light.

{"count":1,"value":{"Message":"Error converting value \"query\" to type 'Microsoft.TeamFoundation.WorkItemTracking.Web.Models.Wiql'. Path '', line 1, position 7.\r\n"}}

Solution

  • Thanks a lot for help me out. I was able to figure it out and fix this issue. I have to use "json" instead of "body" to fix this issue (Please check below code ..However I still get same error in postman . Still trying to figure-out why its giving error).

     var options = {            
                        url: colURL,
                        username: this.username,
                        password: this.password,
                        domain: this.domain,
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        }, 
                        json :{
                            'query': query
                        }         
                    };