Search code examples
azurepowershellazure-devopsazure-devops-wiki

How to create Azure Devops Wiki SubPage with PowerShell


I need to create a sub page in Azure Devops Wiki.

Here is the following code which create the main page and if i change the name the new page is created but not a following sub page. There is no clear information to order or create sub page in official docs https://learn.microsoft.com/fr-fr/rest/api/azure/devops/wiki/pages?view=azure-devops-rest-6.0 How can i achieve this?

$content = [IO.File]::ReadAllText("wiki\file.md")
$data= @{content=$content;} | ConvertTo-Json;
$OrganizationName = "organizationName"
$ProjectName = "ProjectName"
$WikiName = "WikiName"
$WikiPath = "MainPage"
$PAT="PAT Token"

$uri = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/wiki/wikis/$WikiName/pages?path=$WikiPath&api-version=6.0"

$Header = @{
    'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
}

$params = @{
    Uri = $uri;
    Headers    = $Header;
    Method     = 'Put';
    ContentType = "application/json";
    body = $data;
}

Invoke-RestMethod @params

Solution

  • From your Powershell Sample and Rest API url, you need to add the main page path to the path in the url.

    https://dev.azure.com/$OrganizationName/$ProjectName/_apis/wiki/wikis/$WikiName/pages?path=MainPagePath/$WikiSubPagePath&api-version=6.0
    

    For example:

    $content = [IO.File]::ReadAllText("wiki\file.md")
    $data= @{content=$content;} | ConvertTo-Json;
    $OrganizationName = "organizationName"
    $ProjectName = "ProjectName"
    $WikiName = "WikiName"
    $WikiPath = "MainPage"
    $PAT="PAT Token"
    
    $uri = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/wiki/wikis/$WikiName/pages?path=$WikiPath/$WikiSubPagePath&api-version=6.0"
    
    $Header = @{
        'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    }
    
    $params = @{
        Uri = $uri;
        Headers    = $Header;
        Method     = 'Put';
        ContentType = "application/json";
        body = $data;
    }
    
    Invoke-RestMethod @params
    

    Then you can create a subpage under the main page.