Search code examples
pythonazure-devopsado

Python - azure-devops - AssignedTo field is missing


I wrote a python function retrieving the results from an ADO query

def get_query_results(connection, project_name,query_id):

   '''
   Gets the result of a query in ADO
   '''
   results = []
   try:
      wit_client = connection.clients.get_work_item_tracking_client()
      result = wit_client.query_by_id(query_id)

      work_items = result.work_items

      for work_item in work_items:
         work_item_details = wit_client.get_work_item(id=work_item.id, fields=["System.Id", "System.Title","System.AssignedTo"], expand="Relations,Fields")
         results.append({"id": work_item_details.fields['System.Id'], "title": work_item_details.fields['System.Title'], "assigned_to": work_item_details.fields['System.AssignedTo']})
   except Exception as e:
        logger.exception(f'Error fetching repositories from {project_name}: {str(e)}')

   return results

but although there are a lot of available fields like "Assigned To" in ADO ui, it seems I cannot get AssignedTo field

results.append({"id": work_item_details.fields['System.Id'], "title": work_item_details.fields['System.Title'], "assigned_to": work_item_details.fields['System.AssignedTo']})
KeyError: 'System.AssignedTo'
  • how can I fix this ?
  • how can I retrieve the available fields instead of just guessing ?

thanks for your help


Solution

  • When using the function "wit_client.get_work_item" to get a work item in Python, if this work item is not assigned to any user, the output of the function will not contain the 'System.AssignedTo' field. In this situation, the "work_item_details.fields['System.AssignedTo']" will return the error message "KeyError: 'System.AssignedTo'".

    To avoid this error, you can try to use the function like as below.

    work_item = wit_clinet.get_work_item(xxx)
    work_item_assigned_to = work_item.fields.get('System.AssignedTo')
    
    • If the work item has been assigned to a user, it will return the content of 'System.AssignedTo'.
    • If the work item is not assigned to any user, it will return 'None' instead of an error.

    EDIT:

    Below is the Python script sample I tested on my side, and it can work well as stated above:

    from azure.devops.connection import Connection
    from msrest.authentication import BasicAuthentication
    
    personal_access_token = '{personal_access_token}'
    organization_url = 'https://dev.azure.com/{organization}'
    query_id = '{query_id}'
    
    credentials = BasicAuthentication('', personal_access_token)
    connection = Connection(base_url=organization_url, creds=credentials)
    
    wit_clinet = connection.clients.get_work_item_tracking_client()
    query_result = wit_clinet.query_by_id(query_id)
    
    work_items = query_result.work_items
    for wit in work_items:
        print('--------------------------------------------------------------')
        print(f'Getting work item by Id: {wit.id}')
        work_item = wit_clinet.get_work_item(id=wit.id, fields=["System.Id", "System.Title","System.AssignedTo"], expand="Relations,Fields")
        work_item_assigned_to = work_item.fields.get('System.AssignedTo')
        print(work_item_assigned_to)