TFS Code Search/Work Rest API return 404

2.1k Views Asked by At

we are using TFS on-premise.

TFS version: Microsoft Visual Studio Team Foundation Server Version 16.122.27409.2 (2018).

We need to perform TFS source control (Code Search) According to MS API documentation this the way to use TFS REST API. Build and Release API are working, but search API return 404. Search Code extension installed and working fine from TFS portal.

API Url:

POST: http://{DNS}:8080/tfs/{Collection}/{Project}/_apis/search/codesearchresults?api-version=4.1-preview.1

the result: Search code result

Please help, what I'm, doing wrong?

2

There are 2 best solutions below

0
On

You can't just open it in a browser. You have to provide a request body, as expressed clearly in the API examples:

{
  "searchText": "CodeSearchController",
  "$skip": 0,
  "$top": 1,
  "filters": {
    "Project": [
      "MyFirstProject"
    ],
    "Repository": [
      "MyFirstProject"
    ],
    "Path": [
      "/"
    ],
    "Branch": [
      "master"
    ],
    "CodeElement": [
      "def",
      "class"
    ]
  },
  "$orderBy": [
    {
      "field": "filename",
      "sortOrder": "ASC"
    }
  ],
  "includeFacets": true
}
4
On

Just as Daniel said "You can't just open it in a browser. You have to provide a request body"

So you can use tools such as Postman to send the request with request body, or you can use PowerShell to call the REST API with request body.

Besides, based on my test it seems the REST API you mentioned (Code Search Results) is not apply to on-premise TFS. I tested on TFS 2018 Update2 (Version 16.131.27701.1), it always return "count": 0,.

However you can use below REST API to search code:

POST http://server:8080/tfs/DefaultCollection/{Project}/_api/_search/postCodeQuery?api-version=4.1-preview.1

Request Body:

{"searchText":"<test1>",
 "scope":"Team Foundation Server",
 "filters":"{\"ProjectFilters\":[\"0511ScrumTFVC\"]}",
 "skipResults":0,
 "takeResults":50,
 "sortOptions":""   
}

Below PowerShell sample for your reference:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection",  
   [string]$projectName = "ProjectName",
   [string]$user = "username",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


function CreateJsonBody
{

    $value = @"

{"searchText":"<test1>",
 "scope":"Team Foundation Server",
 "filters":"{\"ProjectFilters\":[\"ProjectName\"]}",
 "skipResults":0,
 "takeResults":50,
 "sortOptions":""   
}

"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/$($projectName)/_api/_search/postCodeQuery?api-version=4.1-preview.1"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$result = $result | convertto-json

Write-host $result 

enter image description here