Edit VSTS Wiki page via VSTS API

荒凉一梦 提交于 2019-12-24 12:03:24

问题


I am trying to edit a VSTS wiki page via powershell and VSTS API and I am using this documentation as a reference.

When I try to edit an existing page, I get the following error:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The page '<PAGE That I CREATED ALREADY>' 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}

At line:32 char:11
+ $result = Invoke-RestMethod -Uri $uri -Method Put -ContentType "appli ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I can create a new page with the content there, but I am stumped as how would I update an existing wiki page. Please help.

Snippet of My Code:

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

    # updates wiki page

$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/wiki/wikis/$($wikiIdentifier)/pages?path=/$($existingPage)&api-version=4.1"

$body = @"
    {
        "content": "Hello"
    }
"@

$result = Invoke-RestMethod -Uri $uri -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body -Verbose

回答1:


As the post Update wiki page REST API can not working in developer community, please make sure If-Match is contained in request header.




回答2:


May I suggest a different approach?

The storage for a VSTS Wiki is a Git repo (see here). So you can clone the repo, change the files and push your changes back.

You can even use a VSTS build for this: the clone is implicit and there are many extensions to implement the push.




回答3:


Make sure that the page version is included in the request header. Refer to this link for details: Pages - Create Or Update.

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.



来源:https://stackoverflow.com/questions/51216446/edit-vsts-wiki-page-via-vsts-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!