2015-12-02 57 views

回答

3

好工作項查詢導出到Excel,找到一種方式來完成這項工作。我再看看API文檔。頁面https://www.visualstudio.com/en-us/docs/integrate/api/wit/overview幫助我。你首先需要做

GET https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/queries/{folderpath}?api-version={version}&$expand=wiql 

從你需要得到wiql的一部分,這是實際的查詢產生的JSON。在此之後,你需要做一個

POST https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version={version} 

在身體與{「查詢」 =「YOURQUERY」}

的JSON因此,您會收到包含所有工作項的網址/ ID的JSON。你需要通過他們去,並通過

GET URL?$expand=all 

通知查詢每個單獨的工作項:加$擴大=所有隻有當你需要的關係和附件嗎?。 我把一些東西放在一起用於PowerShell。注意:我決定對查詢進行硬編碼,並刪除錯誤處理以使其縮短一點。

function loadJsonFile($fileName) 
{ 
    return ConvertFrom-Json "$(Get-Content $fileName)" 
} 
function getLastItemFromURL($url) 
{ 
    $absPath = ([System.Uri]$url).AbsolutePath 
    $lastSlash = $absPath.LastIndexOf("/") 
    $absPath.Substring($lastSlash+1) 
} 
function getWorkItemId($url) 
{ 
    getLastItemFromURL($url) 
} 

# make sure you enabled alternative credentials and access for them 
# you can get the value for YOURCODE i.e. via Fiddler 
$headers = @{Authorization="Basic YOURCODE"} 

# before this you would need to find the WIQL of the query; left to you 
$body = @{ 
    "query" = "THEQUERYFROMTHEJSON" 
} 
$bodyJson = $body | ConvertTo-Json 

Invoke-RestMethod -method Post -ContentType application/json -Uri "https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version=1.0" -Headers $headers -Body $bodyJson -OutFile workitems.json 

$workItemsJson = $(loadJsonFile workitems.json) 
$workItems = $(foreach ($relation in $workItemsJson.workItemRelations) 
{ 
    $relation.target.url 
    $relation.source.url 
}) | select -Unique | sort 

echo "Going to download the following ids from $(getWorkItemId $workItems[0])-$(getWorkItemId $workItems[-1])" 

# download the workitems 
foreach($workItemUrl in $workItems) 
{ 
    $workItemId = getWorkItemId $workItemUrl 
    echo "Download ID: $workItemId" 

    $workItemUrl = "$workItemUrl`?`$expand=all" 
    $fileName = "workitem_$workItemId.json" 
    Invoke-RestMethod -ContentType application/json -Uri "$workItemUrl" -Headers $headers -OutFile "$fileName" 

    # download attachments 
    $workItemJson = $(loadJsonFile "$fileName") 
    foreach($relation in $workItemJson.relations) 
    { 
     if($relation.rel -eq "AttachedFile") 
     { 
      $fileUrl = $relation.url 
      Invoke-WebRequest $fileUrl -Headers $headers -OutFile $(getLastItemFromURL $fileUrl) 
     } 
    } 
} 
1

你可以使用的Team Foundation加載到Excel中描述here

+0

我需要一些你可以在powershell腳本中執行的東西。理想情況下沒有安裝Excel。 – SACO