0

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?

Community
  • 1
  • 1
PrimeNum
  • 53
  • 6

2 Answers2

1

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
}
Daniel Mann
  • 49,975
  • 11
  • 87
  • 105
0

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

Andy Li-MSFT
  • 24,802
  • 2
  • 22
  • 40
  • Hi Andy. Thank you for your detailed response. However I can't execute the example as you described. I'm using Postman and PAT authentication. When I'm executing search request I receive error 500, The required anti-forgery form field "__RequestVerificationToken" To overcame this issue i need to send __RequestVerificationToken as form and not as json request. Do u have any idea why? Can it be some TFS server configurations? – PrimeNum May 27 '18 at 11:27
  • @PrimeNum What's that mean for "`However I can't execute the example as you described`"? what errors appear when you run the PS script or any other concerns? Have you set the correct scopes for the PAT? Could you please share the full error message or capture a screenshot, then we can troubleshoot accordingly. – Andy Li-MSFT May 28 '18 at 02:31
  • Hi Andy. I'm trying to execute **Code Search** using Postman. I'm uploaded some elaborated information here [link]https://drive.google.com/drive/folders/1F3nCrJfPbIUSQIFwi_zw6qFxJJ_idb0S?usp=sharing. I hope you can take a look. – PrimeNum May 28 '18 at 10:49
  • @PrimeNum Cannot reproduce this issue on my side, everything working as expected. However based on the error message, you can try to [enable cookies in your browser](https://my.iit.edu/jsp/misc/java_cook.jsp) (IE in your scenario), also add the TFS address to trust site, then check it again. – Andy Li-MSFT May 29 '18 at 03:16