Search code examples
pythonazure-devopswiql

How to retrieve wiki links attached to a Work Item


I am trying to extract wiki links from Azure DevOps using the azure-devops python package. Is this something feasible to do using WIQL? Retrieving item-to-item links can be done like this:

        SELECT * FROM workitemLinks 
        WHERE (Source.[System.AreaPath] Under 'devOpsTesting\\testArea')
        AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward')
        AND (Source.[System.Id] = 3)  
        ORDER BY [System.Id]
        MODE (Recursive)
    

Is there a similar solution for wiki links, if not how would one retrieve them?


Solution

  • We cannot list the work links for wiki links via WIQL query. If the link does not contain work items, we could not get the result we want.

    As a test result, I found that if we link wiki to work item, the value of field External Link Count will be greater than 0.

    As a workaround, we could use REST API to list all work items that External Link Count is greater than 0.

    REST API

    POST https://dev.azure.com/{Org name}/{Project name}/{Team name}/_apis/wit/wiql?api-version=6.0
    

    Request Body:

     {"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' AND [External Link Count] > '0' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
    }
    

    Now, we could list work item IDs.

    Result:

    enter image description here

    Then we check the work item detail info via REST API.

    GET https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/{Work item ID}?$expand=all&api-version=6.1-preview.3
    

    We need notice the field relations.attributes.name, If the work item links wiki, we could check it via this field.

    Result:

    enter image description here

    Update1

    retrieve wiki links attached to a Work Item

    List work item IDs via REST API and power shell.

    Power shell script:

    $connectionToken="{PAT}"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $WorkItemQueryURL = "https://dev.azure.com/{Org name}/{project}/{team name}/_apis/wit/wiql?api-version=6.0" 
    
    $body =@"
    {
      "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' AND [External Link Count] > '0' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
    }
    "@
    $WorkItem = Invoke-RestMethod -Uri $WorkItemQueryURL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
    
    #Write-host $WorkItem.workItems.id
    
    
    
    ForEach ($ID in $WorkItem.workItems.id)
    {
     
     $WorkItemInfoURL = "https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/$($ID)?" + "$" + "expand=1&api-version=6.1-preview.3" 
     $WorkItemInfo = Invoke-RestMethod -Uri $WorkItemInfoURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
    
     #Write-host   $WorkItemInfo.relations.attributes.name
    
     if ($WorkItemInfo.relations.attributes.name -eq "Wiki Page"){  
      Write-host $ID
     }
    
    }
    

    Result:

    enter image description here

    Update2

    Rest API:

    GET https://dev.azure.com/{Org name}/{project name}/_apis/wit/workitems/{wok item ID}?$expand=all&api-version=6.1-preview.3
    

    Result:

    We could get the wiki name via the response url link.

    enter image description here