Search code examples
c#azure-devopswikiazure-devops-wiki

Azure DevOps: Wiki page with WikiHttpClient not found


I created a project wiki in Azure DevOps and want to get the wiki markdown pages in my .NET application. When using the link

https://dev.azure.com/company/project/_apis/wiki/wikis/KIS.wiki/pages/News

The markdown gets shown in the browser. When I try to do that in code, I am getting

"Wiki page ‘/News/_apis/connectionData’ could not be found. Ensure that the path of the page is correct and the page exists."

My code looks like this:

var url = new Uri("https://dev.azure.com/company/project/_apis/wiki/wikis/KIS.wiki/pages/News");
var personalAccessToken = "xxxxxxxxxxxxxxxx";
var credentials = new VssCredentials(new VssBasicCredential("", personalAccessToken));

using (var connection = new VssConnection(url, credentials))
{
    var wikiClient = connection.GetClient<WikiHttpClient>();
    var markdown = wikiClient.GetWikiAsync("KIS.wiki").Result;
}

The error appears on GetClient().

What am I doing wrong?


Solution

  • I see a couple problems with how you're trying to get the page content.

    1. The url given to the connection should be the "project base path"
      private static string BasePath = $"https://dev.azure.com/{Organization}";
    2. You're using the GetWikiAsync(...) method when you want to be using the GetPageAsync(...) method

    Here's an example

    private readonly IVssCredentialsFactory _credentialsFactory;
    
    private const string ApiVersion = "5.1";
    private static string BasePath = $"https://dev.azure.com/{Organization}";
    private const string Organization = "company";
    private const string Project = "project";
    
    public AzureRepository(IVssCredentialsFactory credentialsFactory)
    {
        _credentialsFactory = credentialsFactory;
    }
    
    public void GetWikiPage()
    {
        using (var connection = new VssConnection(new Uri(BasePath), _credentialsFactory.GetCredentials()))
        {
            var wikiClient = connection.GetClient<WikiHttpClient>();
            var wikiId = "KIS.wiki";
            var path = "/News";
            var page = wikiClient.GetPageAsync(Project, wikiId, path, includeContent : true).Result;
            var content = page.Page.Content;
        }
    }
    

    notes about this sample:

    • IVssCredentialsFactory is my creation, so don't look for it in the lib
    • The injection of the factory is were the PAT or oAuth token is, so don't think you're doing anything wrong there. You're not.
    • I hope it's obvious that the method isn't doing anything with the result, b/c let's face it, it's a sample.

    If you're not already

    You should look at the c# client samples. It's not exhaustive, but can be helpful.