How to Import Azure Wiki Contents into a JSON File

Here you need

Before we dive into the implementation, ensure you have the following prerequisites as well.

Azure DevOps Account

Make sure you have access to an Azure DevOps account with permission to read wiki content. You can create an Azure free account via https://azure.microsoft.com/en-gb/free

Personal Access Token (PAT)

Generate a PAT in your Azure DevOps account with appropriate permissions to access the wiki content via the REST API.

To create a PAT, once you have logged in to your DevOps portal https://dev.azure.com/ go.

Create PAT

From here you can define the period for the token activation level of permissions. Once you are done it will create you a token to use in your script.

Once above all done.

Add the below script in POSTMAN:

https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages/1?includeContent=True&api-version=7.1-preview.1

When you add the above script you need to set a few parameters as below.

 Parameters

When you insert the script remember to set the method as GET.

 Insert the script

Set the authorization to basic authentication leave the User name blank and include the PAT token.

Set authorization

Once you are done click the send and you will see your Wiki content as the results.

After a successful response from the POSTMAN tool, you can create a PYTHON script.

You can use the below script and this will generate a JSON file but remember this will come in a basic text format since most of Azure Wiki content is written in basic markdown format.

import requests
import json
import base64

# Azure DevOps credentials and parameters
organization = '{organization}'
project = '{project}'
wiki_identifier = '{wikiIdentifier}'
api_version = '7.1-preview.1'
pat = 'PAT token'
username = "dummy"
combined_pat_token = username + ":" + pat

# API endpoint
url = f'https://dev.azure.com/Kreepertestlab/Wiki/_apis/wiki/wikis/WikitoDB.wiki/pages/1?includeContent=True&api-version=7.1-preview.1'

# Request headers with authorization
headers = {
    'Authorization': b'Basic ' + base64.b64encode(combined_pat_token.encode('utf-8')),
    'Content-Type': 'application/json'
}

# Send GET request to Azure DevOps Wiki API
response = requests.get(url, headers=headers)

# Check if request was successful
if response.status_code == 200:
    # Parse JSON response
    wiki_content = response.json()
    content_value = wiki_content['content'] if 'content' in wiki_content else None

    if content_value is not None:
    # Save content value to a file
        with open('wiki_content.txt', 'w') as file:
            file.write(content_value)
            print("Content value saved to 'wiki_content.txt'")
    else:
        print("Error: No content found in the response.")
else:
    print(f"Error: Failed to retrieve Wiki content. Status code: {response}")
    print(f"Response body: {response}")

Note. You may see am using a dummy account as.

  • username = “dummy”
  • combined_pat_token = username + “:” + pat

This is because we can not send the PAT token directly and it will return a 203 authentication error. Suggest to me if you have a better way to do it.

Let me know if you have any doubts or better ways we can do this.


Similar Articles