0

My Azure devops page will look like :

enter image description here

I have 4 pandas dataframes. I need to create 4 sub pages in Azure devops wiki from each dataframe. Say, Sub1 from first dataframe, Sub2 from second dataframe and so on.

My result should be in tab. The result should look like :

enter image description here

Is it possible to create subpages thru API? I have referenced the following docs. But I am unable to make any sense. Any inputs will be helpful. Thanks.

https://github.com/microsoft/azure-devops-python-samples/blob/main/API%20Samples.ipynb https://learn.microsoft.com/en-us/rest/api/azure/devops/wiki/pages/create%20or%20update?view=azure-devops-rest-6.0

usr_lal123
  • 650
  • 12
  • 28

3 Answers3

1

Able to achieve with rest api

import requests
import base64
import pandas as pd

pat = 'TO BE FILLED BY YOU'  #CONFIDENTIAL
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}

df = pd.read_csv('sf_metadata.csv')  #METADATA OF 3 TABLES 
df.set_index('TABLE_NAME', inplace=True,drop=True)
df_test1 = df.loc['CURRENCY'] 


x1 = df_test1.to_html()  # CONVERTING TO HTML TO PRESERVE THE TABULAR STRUCTURE

#JSON FOR PUT REQUEST
SamplePage1 = {
  "content": x1
}

#API CALLS TO AZURE DEVOPS WIKI 
response = requests.put(
    url="https://dev.azure.com/xxx/yyy/_apis/wiki/wikis/yyy.wiki/pages?path=SamplePag2&api-version=6.0", headers=headers,json=SamplePage1)
print(response.text)
usr_lal123
  • 650
  • 12
  • 28
  • You could [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Apr 27 '21 at 09:16
  • @Cece Dong. Done. Can you please check if any of your team members can assist on this https://stackoverflow.com/questions/67282459/perform-azure-purview-scan-thru-api. Thanks – usr_lal123 Apr 27 '21 at 11:53
1

Based on @usr_lal123's answer, here is a function that can update a wiki page or create it if it doesn't:

    import requests
    import base64
    
    pat = ''  # Personal Access Token to be created by you
    authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
    
    def update_or_create_wiki_page(organization, project, wikiIdentifier, path):
        # Check if page exists by performing a Get
        headers = {
        'Accept': 'application/json',
        'Authorization': 'Basic '+authorization
        }
        response = requests.get(url=f"https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}&api-version=6.0", headers=headers)
        
        # Existing page will return an ETag in their response, which is required when updating a page    
        version = ''
        if response.ok:
            version = response.headers['ETag']
    
        # Modify the headers
        headers['If-Match'] = version
        
        pageContent = {
            "content": "[[_TOC_]] \n ## Section 1 \n normal text"
            + "\n ## Section 2 \n [ADO link](https://azure.microsoft.com/en-us/products/devops/)"
        }
    
        response = requests.put(
            url=f"https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}&api-version=6.0", headers=headers,json=pageContent)
        print("response.text: ", response.text)
Esset
  • 916
  • 2
  • 15
  • 17
0

To create a wiki subpage, you should use Pages - Create Or Update api, and specify the path to pagename/subpagename. Regarding how to use the api in Python, you could use Azure DevOps Python API and refer to the sample below:

def create_or_update_page(self, parameters, project, wiki_identifier, path, version, comment=None, version_descriptor=None):
        """CreateOrUpdatePage.
        [Preview API] Creates or edits a wiki page.
        :param :class:`<WikiPageCreateOrUpdateParameters> <azure.devops.v6_0.wiki.models.WikiPageCreateOrUpdateParameters>` parameters: Wiki create or update operation parameters.
        :param str project: Project ID or project name
        :param str wiki_identifier: Wiki ID or wiki name.
        :param str path: Wiki page path.
        :param String version: Version of the page on which the change is to be made. Mandatory for `Edit` scenario. To be populated in the If-Match header of the request.
        :param str comment: Comment to be associated with the page operation.
        :param :class:`<GitVersionDescriptor> <azure.devops.v6_0.wiki.models.GitVersionDescriptor>` version_descriptor: GitVersionDescriptor for the page. (Optional in case of ProjectWiki).
        :rtype: :class:`<WikiPageResponse> <azure.devops.v6_0.wiki.models.WikiPageResponse>`
        """
        route_values = {}
        if project is not None:
            route_values['project'] = self._serialize.url('project', project, 'str')
        if wiki_identifier is not None:
            route_values['wikiIdentifier'] = self._serialize.url('wiki_identifier', wiki_identifier, 'str')
        query_parameters = {}
        if path is not None:
            query_parameters['path'] = self._serialize.query('path', path, 'str')
        if comment is not None:
            query_parameters['comment'] = self._serialize.query('comment', comment, 'str')
        if version_descriptor is not None:
            if version_descriptor.version_type is not None:
                query_parameters['versionDescriptor.versionType'] = version_descriptor.version_type
            if version_descriptor.version is not None:
                query_parameters['versionDescriptor.version'] = version_descriptor.version
            if version_descriptor.version_options is not None:
                query_parameters['versionDescriptor.versionOptions'] = version_descriptor.version_options
        additional_headers = {}
        if version is not None:
            additional_headers['If-Match'] = version
        content = self._serialize.body(parameters, 'WikiPageCreateOrUpdateParameters')
        response = self._send(http_method='PUT',
                              location_id='25d3fbc7-fe3d-46cb-b5a5-0b6f79caf27b',
                              version='6.0-preview.1',
                              route_values=route_values,
                              query_parameters=query_parameters,
                              additional_headers=additional_headers,
                              content=content)
        response_object = models.WikiPageResponse()
        response_object.page = self._deserialize('WikiPage', response)
        response_object.eTag = response.headers.get('ETag')
        return response_object

More details, you could refer to the link below:

https://github.com/microsoft/azure-devops-python-api/blob/451cade4c475482792cbe9e522c1fee32393139e/azure-devops/azure/devops/v6_0/wiki/wiki_client.py#L107

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Yes. I have gone thru this. But I am unable to get the full flow - authentication, parameters(examples would be helpful) required for the method. – usr_lal123 Apr 13 '21 at 10:09