Forum Discussion

Abdul_ur_Rehman's avatar
Abdul_ur_Rehman
Copper Contributor
Jan 10, 2024

How to revert back WorkItems' old component after changing them to new component

I mistakenly changed the components of different areas to a single component. I created a query for components without realizing that there was a mistake in the query and changed the components of all the areas to a single component. Because of this 13K worktimes got updated.

It is not possible to change each item manually. Is there any way to restore the old components of those work items?

 

I explored the APIs which return the list of all the work items in the query. Also, I explored the API which retrieves the information for old and new component values. I am not sure how can I change back to the values of the old components for all 13K work items.

 

 

Get work item history API:

https://dev.azure.com/astera/Centerprise/_apis/wit/workItems/%7Bid%7D/updates?api-version=5.1

Get work item ids from query:
https://dev.azure.com/astera/Centerprise/_apis/wit/wiql/%7Bqueryid%7D?api-version=6.0

1 Reply

  • How about this:

     

    1. Get Affected Work Item IDs

    Use your WIQL query to get the list of affected work items:

    GET https://dev.azure.com/{organization}/{project}/_apis/wit/wiql/{queryId}?api-version=6.0

    Extract the workItemIds from the response.

     

    2. Fetch Work Item History

    For each work item ID, call:

    GET https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{id}/updates?api-version=5.1

     

    • Look for the update where the Component field (or whatever custom field name you're using) was changed.
    • Extract the old value from the fields["System.AreaPath"].oldValue or your custom field.

     

    3. Prepare a Patch Payload

    Once you have the old value, use a PATCH request to update the work item:

    PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.0
    Content-Type: application/json-patch+json
    
    [
      {
        "op": "add",
        "path": "/fields/Custom.Component",
        "value": "OldComponentValue"
      }
    ]

    Replace Custom.Component with your actual field reference name.

     

    4. Automate with a Script

    Use PowerShell, Python, or a tool like Postman Collection Runner to automate this for all 13K items. Here's a Python snippet using requests:

    import requests
    from requests.auth import HTTPBasicAuth
    
    PAT = 'your_personal_access_token'
    headers = {
        'Content-Type': 'application/json-patch+json'
    }
    
    def revert_component(work_item_id, old_value):
        url = f"https://dev.azure.com/your_org/your_project/_apis/wit/workitems/{work_item_id}?api-version=6.0"
        payload = [{
            "op": "add",
            "path": "/fields/Custom.Component",
            "value": old_value
        }]
        response = requests.patch(url, json=payload, headers=headers, auth=HTTPBasicAuth('', PAT))
        return response.status_code
    
    # Loop through your work items and call revert_component

     

Resources