Use PowerShell to Get Wiki Page Content from Azure DevOps REST API

Use PowerShell to Get Wiki Page Content from Azure DevOps REST API

This article details how to use PowerShell to send a request to Azure DevOps REST API to fetch the content of a Wiki Page.


1. Obtain a Personal Access Token (PAT)

Before we get to PowerShell, you first need a Personal Access Token (PAT) from Azure DevOps. This token allows you to communicate fully with Azure DevOps API.

To obtain a PAT, click the gear & person icon at the top right of Azure DevOps and select the Personal access tokens option.

On the following page, you'll be able to create a New Token after filling out a form. Once you obtain a PAT, Copy & Paste the value to somewhere safe for the time being.

2. Set Variables

These are all the variables you'll use to construct the request URL. Replace the values below with those specific to your own Azure DevOps and the Wiki Page you want to fetch the contents of.

# Azure DevOps account details
$organizationName = "OrgName"
$projectName = "ProjName"
$wikiName = "WikiName"
$pageId = "1234"
$apiVersion = "7.0"

3. Authenticating with the API

To authenticate with Azure DevOps REST API, in the request headers you'll need to provide the PAT but in base64 encoding. (The base64 value will be provided to the headers during the request statement in the next step.)

# Personal Access Token (PAT) for authentication
$personalAccessToken = "<pat-you-generated-in-step-1>"

# Base64 encode the PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))

4. Sending the Request and Displaying the Results

Be sure to include &includeContent=true at the end of the URL.

# Construct the URL for the REST API call
$url = "https://dev.azure.com/$($accountName)/$($projectName)/_apis/wiki/wikis/$($wikiName)/pages/$($pageId)?api-version=$($apiVersion)&includecontent=true"

# Make the REST API call
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get

# Print to screen the Wiki Page content
$response.Content

And that's it! You've now successfully used PowerShell to fetch the contents of an Azure DevOps Wiki Page via REST API.


The Entire Script

# Azure DevOps account details
$organizationName = "OrgName"
$projectName = "ProjName"
$wikiName = "WikiName"
$pageId = "1234"
$apiVersion = "7.0"

# Personal Access Token (PAT) for authentication
$personalAccessToken = "<the-pat-you-generated-from-step-1>"

# Base64 encode the PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))

# Construct the URL for the REST API call
$url = "https://dev.azure.com/$($accountName)/$($projectName)/_apis/wiki/wikis/$($wikiName)/pages/$($pageId)?api-version=$($apiVersion)&includecontent=true"

# Make the REST API call
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get

# Output the Wiki Page content
$response.content

Please leave a comment if you have feedback or questions!

Thanks!


Reference