user9473385
user9473385

Reputation: 131

How to list all the files that are in TFS GIT repo using REST API

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

Upvotes: 7

Views: 7724

Answers (3)

Danny Frencham
Danny Frencham

Reputation: 931

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

Upvotes: 2

unsafePtr
unsafePtr

Reputation: 1763

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.

Upvotes: 3

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31003

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

Upvotes: 6

Related Questions