Search code examples
azure-devopspower-automateazure-devops-rest-apiemail-notificationsazure-boards

Send daily email of Azure DevOps work items updated in the last 24 hours (Bulk)


I'd like to create a daily notification email, consisting of all of the work items which have received either a new comment in the discussion section, had their status changed, were assigned to someone or had any of the other fields (Description, Effort, Due Date, etc.) changed.

I would like to compile these work items into an E-mail, but also include what the update itself was, and send all of the work items in one compiled email.

I know that it's possible to create a query which lists items where the 'Changed Date' variable is in the last 24 hours, but to the best of my knowledge the query tool cannot list out what action was performed specifically.

I'm currently doing this in a brand-new project environment, there are no custom variables set, no rules specified for work items, nothing, a blank canvas.

Thank you for your input!


Solution

  • I'd like to create a daily notification email, consisting of all of the work items which have received either a new comment in the discussion section, had their status changed, were assigned to someone or had any of the other fields (Description, Effort, Due Date, etc.) changed.

    Based on your requirement, you need to get the work item list which are changed in the last 24 hours and get the change details.

    I am afraid that there is no out-of-box method can directly get the work item list and the changed details.

    To meet your requirement, you can consider using PowerShell script to run Rest APIs to get the changed work item list and the change details in the last 24 hours.

    Here is the sample code:

    $token = "PAT"
    
    $url="https://dev.azure.com/{Orgname}/_apis/wit/wiql?api-version=5.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $JSON = @'
    {
      
      "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.ChangedDate] >= @today-1  order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
    
    }
    '@
    
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
    
    foreach($workitemid in $response.workItems.id)
    { 
      echo $workitemid
    
      $url1="https://dev.azure.com/{Orgname}/_apis/wit/workItems/$($workitemid)/updates?api-version=7.1-preview.3"
    
      $GetUpdates = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method GET  -ContentType application/json
    
      foreach($update in $GetUpdates.value)
      {
    
          $Today= Get-Date $response.asOf
          $Yesterday = $Today.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ss")
    
    
          $Formatchangeddate= (Get-Date $update.fields.'System.ChangedDate'.newValue).ToString("yyyy-MM-ddTHH:mm:ss")
    
          echo $Formatchangeddate
    
         if($Formatchangeddate -gt $Yesterday)
         {
          echo $update.id
          echo $update.fields.'System.History'
         }
      }
    
    }
    

    The sample above is using the Rest APIs: Wiql - Query By Wiql and Updates - List

    It will get the changed work item list based on the work item field:System.changeddate.

    And then it will use the Rest API to loop the changes of the each work item and get the updated fields.

    Result:

    enter image description here

    At present, it only obtains a small amount of relevant information. You can further improve the code according to your needs to obtain more required information.