7

All, I am trying to get the list of all the files that are in a particular repo in TFS GIT using REST API. I found the below one but it only display the contents of the specific file name mentioned after "scopePath=/buld.xml", it only display the contents of file build.xml.

But I am trying, only to list all the files that are in a particular repository with out mentioning the particular file name.

Please help me.

https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/items?items?scopePath=/&api-version=4.1

Community
  • 1
  • 1
user9473385
  • 131
  • 2
  • 8

3 Answers3

6

You can use the api below:

https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/items?recursionLevel=Full&api-version=4.1

enter image description here

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Hello Cece, I tried the one you suggested its not working I used to different API version like 4.0 also still its not working – user9473385 Jun 12 '18 at 01:27
  • 1
    Which version of TFS are you using? What error did you get? I'm testing with VSTS and it works. – Cece Dong - MSFT Jun 12 '18 at 01:30
  • 1
    I am using Version 16.122.27409.2 and its on premise TFS, I am not getting any error but I am not getting what I am looking for. Below is the info I am getting. "count":1,"value":[{"objectId":"id","gitObjectType":"tree","commitId":"ID","path":"/","isFolder":true,"url":"https://URL/_apis/git/repositories/repodi/items//?versionType=Branch&versionOptions=None"}]} – user9473385 Jun 12 '18 at 02:29
  • Please double check whether you specify the correct `project` and `repositoryId` in the api. I just tested with TFS 16.131.27701.1, it also worked. I've added a screenshot for your reference. – Cece Dong - MSFT Jun 12 '18 at 02:51
  • Thanks @CeceDong-MSFT - this did help me. I have the same issue as OP - and I will only see the "/" folder. But If i put the file name simply to the URL I am able to get the files (but not able to list them) e.g. .../items/My_Project.sln?versionType=Branch&versionOptions=None – Eleasar Dec 10 '18 at 11:57
3

Also that could be achieved using VisualStudioOnline libs (at the date of writing comment it becomes AzureDevOps): Microsoft.TeamFoundationServer.Client, Microsoft.VisualStudio.Services.Client.

First, you need to create access token. Then just use code below:

VssBasicCredential credintials = new VssBasicCredential(String.Empty, "YOUR SECRET CODE HERE");
VssConnection connection = new VssConnection(new Uri("https://yourserverurl.visualstudio.com/"), credintials);
GitHttpClient client = connection.GetClient<GitHttpClient>();

List<GitRepository> repositories = await client.GetRepositoriesAsync(true); // or use GetRepositoryAsync()
var repo = repositories.FirstOrDefault(r => r.Name == "Some.Repo.Name");

GitVersionDescriptor descriptor = new GitVersionDescriptor()
{
    VersionType = GitVersionType.Branch,
    Version = "develop",
    VersionOptions = GitVersionOptions.None
};

List<GitItem> items = await client.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.Full, versionDescriptor: descriptor);

Under the hood it's using the REST API. So if you try the same effect using c# lang, better delegate it to lib.

unsafePtr
  • 1,591
  • 2
  • 17
  • 27
2

You need to call the items endpoint first, which gives you an objectId (the gitObjectType should be "tree"):

http://{tfsURL}/tfs/{collectionId}/{teamProjectId}/_apis/git/repositories/{repositoryId}/items?recursionLevel=Full&api-version=4.1

Then call the trees end point to list the objects in the tree:

http://{tfsURL}/tfs/{collectionId}/{teamProjectId}/_apis/git/repositories/{repositoryId}/trees/{objectId}?api-version=4.1

test

Danny Frencham
  • 931
  • 1
  • 8
  • 17