'Azure DevOps Server: create Wiki pages programmatically

I try to generate markdown Wiki pages based on .net xml-documentation for Overview > Wiki in Azure DevOps Server 2019.0.1.

To generate the .md from .xml in each build, I use XmlCommentMarkDownGenerator called by a PowerShell script.

Currently to publish a wikipage I have to upload the .md file manually to the repository and link it over the Publish Code as Wiki option.

enter image description here

Is it possible to publish a .md file programmatically as wikipage inside of a build process without adding it to the main repository? I want to keep wikipages up to date with each build process.

Result looks like:

enter image description here

EDIT: PowerShell Script:

Add-Type -AssemblyName System.Net.Http

#CREATE HTTP CLIENT
$client = New-Object -TypeName System.Net.Http.Httpclient
Write-Host "Request Azure Devops API:" -ForegroundColor Yellow

#PERSONAL ACCESS TOKEN
[string] $PersonalAccessToken = "uwadlzqp6j7i5n35dazsswmwp6gth2w2sxb55h3zrlc2bi7jn5ad";
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))

#CONFIGURE CLIENT
$json = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
$client.DefaultRequestHeaders.Accept.Add($json)
$client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $token)

#REQUEST
$uri = "http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/"
$task = $client.GetAsync($uri)
Write-Host "URI: $uri" -ForegroundColor Yellow

#WAIT FOR THE ASYNC CALL TO FINISH
$task.Wait()

#USE THE RESULT
if ($task.IsCompleted) {

    #DO YOUR THING HERE.
    [System.Net.Http.HttpResponseMessage] $msg = $task.Result
    $result = $msg.Content.ReadAsStringAsync()
    $converted = ConvertFrom-Json –InputObject $result.Result

    #LOOP RESULTS
    for ($i=0; $i -lt $converted.count; $i++) {
        $entry = $converted.value[$i]
        Write-Host "Name:`t $($entry.name)"
        Write-Host "Url:`t $($entry.url)"
        Write-Host

        #REQUEST WIKI
        $task = $client.GetAsync($entry.url + "/pages?")
        $task.Wait()
        [System.Net.Http.HttpResponseMessage] $msg = $task.Result
        $result = $msg.Content.ReadAsStringAsync()
        $converted = ConvertFrom-Json –InputObject $result.Result
        $converted.content = "Hallo"

        $wiki = $entry.name
        #$uri_new = $entry.url + "/pages?api-version=5.0"
        $uri_new = $entry.url + "/pages?pagePath=FrameworkA?api-version=5.0"
        Write-Host "Overwrite Url: $uri_new" -ForegroundColor Red

        $reconvert = ConvertTo-Json –InputObject $converted
        [System.Net.Http.HttpContent] $content = [System.Net.Http.StringContent]::new($reconvert)
        $client.PutAsync($uri_new, $content)


        Write-Host "SECOND TRY: Invoke-RestMethod" -ForegroundColor Green
        $content_type = $client.DefaultRequestHeaders.Accept.ToString()
        $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCredentials -Body @{"content" = "hallo"}
    }
}

The Script try two different method to put/post to the server:

  1. System.Net.Http.Client

  2. Invoke-RestMethod -Method PUT

The output is:

Request Azure Devops API:
URI: http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/
Name:    FrameworkA.wiki
Url:     http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3

Overwrite Url: http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0


Result                 : StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
                         {
                           Pragma: no-cache
                           X-TFS-ProcessId: c79f89a2-e776-41e0-8134-a18dfacad964
                           ActivityId: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-TFS-Session: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-E2EID: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-UserData: d82854f4-53e6-4455-a653-e0c8e665cef1:user
                           X-FRAME-OPTIONS: SAMEORIGIN
                           Lfs-Authenticate: NTLM
                           X-Content-Type-Options: nosniff
                           X-Cache: MISS from ptc-bld-squid
                           X-Cache-Lookup: MISS from ptc-bld-squid:3128
                           Connection: keep-alive
                           Cache-Control: no-cache
                           Date: Mon, 08 Jul 2019 10:53:01 GMT
                           P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
                           Server: Microsoft-IIS/10.0
                           Via: 1.1 ptc-bld-squid (squid/3.5.23)
                           X-AspNet-Version: 4.0.30319
                           X-Powered-By: ASP.NET
                           Content-Length: 92
                           Allow: GET
                           Content-Type: application/json; charset=utf-8
                           Expires: -1
                         }
Id                     : 277695
Exception              : 
Status                 : RanToCompletion
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             : 
IsFaulted              : False
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

SECOND TRY: Invoke-RestMethod
Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
In C:\Users\user\Desktop\Azure DevOps Console\DevOps API Call.ps1:60 Zeichen:21
+ ... $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


Solution 1:[1]

You can create or update a Wiki pages grammatically with Azure DevOps Server Rest API:

PUT https://dev.azure.com/{organization}}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}?api-version=5.0

Body:

{
    "content": "Test Page"
}

So in the build you can add a PowerShell task that use the above API and create the wiki pages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shayki Abramczyk