'Is it possible to have a link to raw content of file in Azure DevOps

It's possible to generate a link to raw content of the file in GitHub, is it possible to do with VSTS/DevOps?



Solution 1:[1]

Even after reading the existing answers, I still struggled with this a bit, so I wanted to leave a bit more of a thorough response.

As others have said, the pattern is (query split onto separate lines for ease of reading):

https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/{{providerName}}/filecontents
  ?repository={{repository}}
  &path={{path}}
  &commitOrBranch={{commitOrBranch}}
  &api-version=5.0-preview.1

But how do you find the values for these variables? If you go into your Azure DevOps, choose Repos > Files from the left navigation, and select a particular file, your current url should look something like this:

https://dev.azure.com/{{organization}}/{{project}}/_git/{{repository}}?path=%2Fpackage.json

You should use those values for organization, project, and repository. For path, you'll see an HTTP encoded version of the unix file path. %2F is the HTTP encoding for /, so that path is actually just /package.json (a tool like Postman will do that encoding for you).

Commit or branch is pretty self explanatory; you either know what you want for this value or you should use master. I have "hard-coded" the api version in the above url because that's what the documentation currently points to.

For the last variable, you need providerName. In short, you should probably use TfsGit. I got this value from looking through the list of source providers and looking for one with a value of true for supportedCapabilities.queryFileContents.

However, if you just request this URL you'll get a "203 Non-Authoritative Information" response back because you still need to authenticate yourself. Referring again to the same documentation, it says to use Basic auth with any value for the username and a personal access token for the password. You can create a personal access token at https://dev.azure.com/{{organization}}/_usersSettings/tokens; ensure that it has the Token Administration - Read & Manage permission.

If you're unfamiliar with this sort of thing, again Postman is super helpful with getting these requests working before you get into the code.


So if you have a repository with a src directory at the root, and you're trying to get the file contents of src/package.json, your URL should look something like:

https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/TfsGit/filecontents?repository={{repository}}&commitOrBranch=master&api-version={{api-version}}&path=src%2Fpackage.json

And don't forget the basic auth!

Solution 2:[2]

I was able to get the raw contents of a file using this URL.

GET https://dev.azure.com/{organization}/{project}/_apis/sourceProviders/{providerName}/filecontents?serviceEndpointId={serviceEndpointId}&repository={repository}&commitOrBranch={commitOrBranch}&path={path}&api-version=5.0-preview.1

I got this from here.

https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/get%20file%20contents?view=azure-devops-rest-5.0

Solution 3:[3]

You can obtain the raw URL using chrome.

Turn on Developer tools and view the Network tab.

Navigate to view the required file in the DevOps portal (Content panel). Once the content view is visible check the network tab again and find the URL which starts with "Items?Path", this is json response which contains the required "url:" element.

Solution 4:[4]

Drag the filename from the attachments windows and drop it in to any other MS application to get the raw URL or linked filename.

Solution 5:[5]

I am fairly new to this and had an issue accessing a raw file in an Azure DevOps Repo. It's straightforward in Github.

I wanted to download a file in CMD and BASH using Curl. First I browsed to the file contents in the browser make a note of the bold sections: https://dev.azure.com/myOrg/_git/myProjectName?path=%2FMyFileName.ps1

I then constructed the URL similar to what @Zach posted above.

https://dev.azure.com/myOrg/myProjectName/_apis/sourceProviders/TfsGit/filecontents?repository=myProjectName&commitOrBranch=master&api-version=5.0-preview.1&path=%2FMyFileName.ps1

Now when I paste the above URL in the browser it displays the content in RAW form similar to GitHub. The difference was I had to setup a PAT (Personal Access Token) in My Azure DevOps account then authenticate the URL in DOS/BASH example below:

curl -u myPATUserName:er90gngekj5er0df "https://dev.azure.com/myOrg/myProjectName/_apis/sourceProviders/TfsGit/filecontents?repository=myProjectName&commitOrBranch=master&api-version=5.0-preview.1&path=%2FMyFileName.ps1" -# -L -o MyFileName.ps1

Solution 6:[6]

Most answers address this well, but in context of a public repo with anonymous access the api is different. Here is the one that works in such a scenario:

https://dev.azure.com/{{your_user_name}}/{{project_name}}/_apis/git/repositories/{{repo_name_encoded}}/items?scopePath={{path_to_your_file}}&api-version=6.0

This is the exact equivalent of the "raw" url provided by Github.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ron
Solution 4 sao
Solution 5
Solution 6 Joerg Krause