-1

I have some problem with Getting workitems from tfs via the following javascript code:

var res = new XMLHttpRequest();

var body = "{'query' : \"select * from workitems where [Change Number] = 'CH-0000433' \"}" ;

res.open("POST","<SERVER_NAME>/tfs/InternalApplications/Testing%20Services/_apis/wit/wiql?api-version=1.0",true,<LOGIN>,<PASSWORD>);

res.setRequestHeader('Content-type', 'application/json');

res.send(body);

when I am trying to execute this script, I am getting the 401 - Unauthorized error

I`ve wrote the PowerShell analogue of this script, and it works fine:

$q2 = """select Id from workitems where [Change Number] = 'CH-0000433' """

$res = Invoke-WebRequest <Server_Name>/tfs/InternalApplications/Testing%20Services/_apis/wit/wiql?api-version=1.0 `
-ContentType application/json `
-Credential $(Get-Credential) `
-Method Post `
-Body "{'query' : $q2}" `
-UseBasicParsing

I suppose that in javascript case I am passing credentials incorrectly, so how should I change it?

  • https://stackoverflow.com/questions/36072327/tfs-2015-rest-api-authentication#36076642 – gvee Mar 29 '18 at 07:58
  • Yes, i have, and it is not working (( - @gvee - of course i tried to implement the solution from stackoverflow.com/questions/36072327/ before creating this post - and it is failed for my case )) – Руслан Багманов Apr 09 '18 at 10:44

1 Answers1

0

I've tested on TFS 2017 with the code snippet below and it is working:

var jsonObj = [{
                "op": "add",
                "path": "/fields/System.Title",
                "value": "cecetest1"
            }];

            $.ajax({
                url: 'http://TFS2017:8080/tfs/DefaultCollection/ScrumProject/_apis/wit/workitems/$Task?api-version=1.0',
                type: 'PATCH',
                contentType: "application/json-patch+json",
                data: JSON.stringify(jsonObj),
                cache: false,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa("domain\\username" + ":" + "password"));
                },
            })
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39