3

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?

JeffK3
  • 107
  • 7

1 Answers1

1

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

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • What I am getting from this is that there is no way to programmatically retrieve the links from a work item to a wiki page using WIQL. Is it safe to assume there is no way to do it with the azure-devops python package/REST API? – JeffK3 Nov 12 '20 at 20:11
  • 1
    Hi @JeffK3, Yes, it is safe to do it with REST API, you just need to protect your PAT, It is better to use the power shell script in the pipeline and set the PAT as a Secret variable. If the answer is helpful for you, you may consider accepting it. Thanks. – Vito Liu Nov 13 '20 at 02:02
  • 1
    Hi @JeffK3, Just checking in to see whether this issue is still blocking you now? Any update for this issue? – Vito Liu Nov 16 '20 at 08:29
  • I'd say I am still blocked, since I am trying to retrieve the names of the wiki pages. And it seems unlikely that the python devops package has the solution I'm looking for. Your answer is useful in that it does tell me I'm looking at this in the wrong way though. – JeffK3 Nov 16 '20 at 14:09
  • 1
    Hi @JeffK3, do you mean that list the work item IDs and link wiki name? If yes, we could use the API shared before to list them. You could check the update2, If I misunderstood you, could you please describe your question in more detail? Thanks. – Vito Liu Nov 18 '20 at 03:32
  • Yes! that is what I was looking for, thank you. It doesn't use the azure-devops package, but I can work with that. – JeffK3 Nov 18 '20 at 23:09