locked
Unable to get custom workitem field using REST-API RRS feed

  • Question

  • Hi all,

    I'm able to get workitem using rest-api; however, when I retrieve custom work item field, it returns nothing. Am I missing something? Please advise.

    [CmdletBinding()]
    Param
    (
        [string]$WorkItemId =$(throw "A work item id is required eg. 45365")
    
    )
    
    # Build environment variables
    #$tpcUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
    #$teamprojectname = $env:SYSTEM_TEAMPROJECT
    #$builddir = $env:BUILD_SOURCESDIRECTORY
    
    # Local envrionment variables
    [string]$teamprojectname = "BuildsRelease"
    [string]$tpcUri = "http://tfs2017.mydomain.local:8080/tfs/devtfs2017"
    [string]$builddir = "C:\DevOps\"
    [string]$workitemlocation = $builddir + "workitem45365.json"
    
    $Uri = $tpcUri + "/" + "/_apis/wit/workitems/" + $WorkItemId + "?api-version=1.0"
    
    Write-Host "URI: $uri"
    
    # Get work item and save to local computer
    $Jsonfile = Invoke-RestMethod -Uri $Uri -Method Get -UseDefaultCredentials
    Write-Host $Jsonfile.fields.mycustomfield.ServerName

    Best Regards,

    Andy Pham


    Best Regards, Andy Pham

    Tuesday, November 7, 2017 12:59 AM

Answers

All replies

  • You need to add expandproperty -fields to be able to access them directly.

    $Jsonfile = Invoke-RestMethod -Uri ($Uri).value -Method Get -UseDefaultCredentials | Select-Object -ExpandProperty Fields
    see example at https://almguide.net/2017/08/30/tfs-queries-in-powershell/#more-120


    • Edited by Kim CarlsenMVP Tuesday, November 7, 2017 6:48 AM typo
    • Marked as answer by AndyPham Tuesday, November 7, 2017 10:10 PM
    Tuesday, November 7, 2017 6:48 AM
  • Hi Andy,

    From my test, if there is a value in custom filed, you will be able to get it from REST api. If the custom filed is empty, you won't see it from the REST api. You may have a try.

    Best regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, November 7, 2017 6:57 AM