Often there is a need to automatically create pages in a Wiki repository, for example:
• To create an iteration report
• Generate a release readme
• And other reporting tasks
Creating a page is quite simple through Rest Api request Pages – Create Or Update. But if you try to refresh the page through the same request, the result is an exception:
{“$id”:”1″,”innerException”:null,”message”:”The page ‘/PageName.md’ specified in the add operation already exists in the wiki. Please specify a new page path.”,”typeName”:”Microsoft.TeamFoundation.Wiki.Server.WikiPageAlreadyExistsException, Microsoft.TeamFoundation.Wiki.Server”,”typeKey”:”WikiPageAlreadyExistsException”,”errorCode”:0,”eventId”:3000}
This is because when updating a request, it is necessary to pass Etag in the request headers. Etag can be obtained through the Rest Api request Pages – Get Page , which returns Etag in the headers. Example:
$restApiUpdateWikiPut = “$orgUrl/$teamProject/_apis/wiki/wikis/$wikiRepoName/pages?path={path}&api-version=6.1-preview.1”
function InvokeGetETag ($GetUrl) { $Headers = $null Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType “application/json” -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} -ResponseHeadersVariable ‘Headers’ return $Headers[“ETag”] } $updateUrl = $restApiUpdateWikiPut.Replace(“{path}”, $wkPagePath) $eTagRaw = InvokeGetETag $updateUrl $ETag = $eTagRaw -join “” |
Next, you need to add Etag to the update page request as “If-Match”=”eTag”:
$restApiUpdateWikiPut = “$orgUrl/$teamProject/_apis/wiki/wikis/$wikiRepoName/pages?path={path}&api-version=6.1-preview.1”
function InvokePutRequest ($PutUrl, $body, $eTag) { return Invoke-RestMethod -Uri $PutUrl -Method Put -ContentType “application/json” -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo);”If-Match”=”$eTag”} -Body $body } InvokePutRequest $updateUrl $wikiContentUpdate $eTag |
The example of a PowerShell script for creating and updating wiki pages can be found here::
https://github.com/ashamrai/AzureDevOpsExtensions/blob/master/CustomPSTasks/UpdateWikiPage.ps1