Is it possible to have a link to raw content of file in Azure DevOps
Asked Answered
C

9

42

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

Cushat answered 10/1, 2019 at 22:46 Comment(1)
Check out How to get a link to a file from a VSO repo, which, I believe, addresses the exact issue.Attila
S
8

Sure, here's the rests call needed:

GET https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feedId}/packages/{packageId}?includeAllVersions={includeAllVersions}&includeUrls={includeUrls}&isListed={isListed}&isRelease={isRelease}&includeDeleted={includeDeleted}&includeDescription={includeDescription}&api-version=5.0-preview.1

https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/artifact%20%20details/get%20package?view=azure-devops-rest-5.0#package

Salivate answered 11/1, 2019 at 5:15 Comment(6)
How do I authenticate to it?Cushat
blog.devmatter.com/calling-vsts-apis-with-powershell lots of articles onlineSalivate
Will it be possible to specify authentication in URL itself? I see all articles requires to have it as part of HTTP header. I need have this URL to be part of ARM template which will not allow any manipulation except on URL stringCushat
not that I'm aware of, but I've been only using this. at worst you can always write a proxy azure functionSalivate
and statically? for example to be used in a wiki markdown file?Enswathe
I've never had the need for this, so I'm not 100% certain, but I think you cannot use URL auth with azure devops, so the answer is probably notSalivate
S
52

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=7.0

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 Build: Read 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!

Schizothymia answered 31/12, 2019 at 19:11 Comment(4)
Great answer, small addition. I generated a personal access token, but it wouldn't work unless I enabled the "Token Administration - Read & Manage" permission. I would have expected the right privilege to be "Code - Read" but that seems to have no effect.Louie
@Louie this comment just saved me from myself :)Penelopepeneplain
do you have an idea how i could change the mime-type of the response from text/plain to text/html?Nomology
As of now you have to set the "Build: Read" token scope to do the trick. "Token Administration" is no longer needed. You can also use the modern api-version=7.0 if you like. The rest of the URL stays the same.Phillisphilly
S
8

Sure, here's the rests call needed:

GET https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feedId}/packages/{packageId}?includeAllVersions={includeAllVersions}&includeUrls={includeUrls}&isListed={isListed}&isRelease={isRelease}&includeDeleted={includeDeleted}&includeDescription={includeDescription}&api-version=5.0-preview.1

https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/artifact%20%20details/get%20package?view=azure-devops-rest-5.0#package

Salivate answered 11/1, 2019 at 5:15 Comment(6)
How do I authenticate to it?Cushat
blog.devmatter.com/calling-vsts-apis-with-powershell lots of articles onlineSalivate
Will it be possible to specify authentication in URL itself? I see all articles requires to have it as part of HTTP header. I need have this URL to be part of ARM template which will not allow any manipulation except on URL stringCushat
not that I'm aware of, but I've been only using this. at worst you can always write a proxy azure functionSalivate
and statically? for example to be used in a wiki markdown file?Enswathe
I've never had the need for this, so I'm not 100% certain, but I think you cannot use URL auth with azure devops, so the answer is probably notSalivate
T
5

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.

Tithe answered 19/4, 2021 at 2:51 Comment(2)
Disagree, several posts above reference obtaining the required parameters however the parameters can easily be obtained from chrome as per the postTithe
My bad. Went through my first set of reviews recently and I'm pretty sure this was one of the first "answers" I reviewed. I must've either misread the question or misread the answer, because yea, this seems fine. Sorry about that and thanks for pointing out my mistake :)Planksheer
D
3

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://learn.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/get%20file%20contents?view=azure-devops-rest-5.0

Dittmer answered 27/6, 2019 at 15:4 Comment(4)
Hi. Please see the help center on how to properly answer questions. stackoverflow.com/help/how-to-answerFrig
I’m not sure why this is considered improper, in that it gives a direct answer and the source for that answer. I had the same question and this was easily the better answer for it.Scotsman
It could be improved by mentioning the need to get a Personal Access Token to use in the authorization header (and where to get that)Scotsman
Hi @AlexWhite so I was trying to do this in order to add a deploy button in a readme file that points at a ARM template (json file). I found yet another API that could get me the raw content but learned that my repo had to be public and not private for this to work. Here's the other API in case you are interested.Dittmer
D
2

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/{{organization_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.

Damarisdamarra answered 27/1, 2022 at 15:21 Comment(2)
Thanks for the edit. Just to clarify: I'm self employeed and run my company under my name and I really have the same (personal) name here for "organization_name". So for some people it might not be that obvious. If one is in an organization, than it's the organization's name, indeed.Damarisdamarra
Note that the returned content-type here is "text/plain". This means unicode characters in the file are not encoded correctly. A checkmark "✅" shows up as "✅" in Firefox and Chrome.Ailina
E
2

So the idea is not to clone the repo but to pick one particular file from the repo, so here is an example of how to do so.

Let us assume this is your Azure DevOps Repo, accessible from the browser.

https://dev.azure.com/<Org>/<Project>/_git/<Repo>?path=<Path>

To access or get the file in your terminal, use this URI.

https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path> 

Windows

Invoke-WebRequest https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path> -Credential <PSCredential>

or

$pair = "<username>:<password>"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
   Authorization = $basicAuthValue
}
$repo = https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>
$downloadPath = "C:\Temp\$fileName"
Invoke-WebRequest -Uri $uri -Headers $Headers 

Linux

curl -u "<username:password>" https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>

Note:

The username will be your Azure DevOps username and the password will be your PAT (personal access token)

Eliathas answered 27/6, 2023 at 5:5 Comment(0)
O
1

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

Oast answered 10/2, 2020 at 12:54 Comment(2)
Can you give example of what MS application you are referring toCushat
Outlook new e-mail. works like a charm.Periphrastic
M
0

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=%2F**MyFileName.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=%2F**MyFileName.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 "<username>:<password>" "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
Moleskin answered 5/6, 2020 at 19:23 Comment(0)
W
0

Another way that may be helpful if you want to quickly get the raw URL for a specific file that you are browsing:

  • install the browser extension named "Undisposition"
  • from the dot menu (top right) choose "Download": the file will open in a new browser tab from which you can copy the URL

(edit: unfortunately this will only work for file types that the browser knows how to open, otherwise it will still offer to download it...)

Wabash answered 26/7, 2022 at 11:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.