Custom Field Script

This script adds custom fields to your Azure DevOps work item type.

        
import requests
import json

# Azure DevOps organization and project information
organization = "YourOrganization"
project = "YourProject"
process_id = "YourProcessId"  # You can find this in the process settings

# Personal Access Token (PAT) for authentication
pat = "YourPersonalAccessToken"

# Define the custom fields and their options
custom_fields = [
    {
        "referenceName": "Custom.TestAttentionPlan",  # Updated reference name
        "name": "Attention",
        "type": "String",
        "pickList": {
            "items": [
                {"value": "Least"},
                {"value": "Average"},
                {"value": "Maximum"}
            ]
        }
    },
    {
        "referenceName": "Custom.Severity",
        "name": "Severity",
        "type": "String",
        "pickList": {
            "items": [
                {"value": "Cosmetic"},
                {"value": "Minor"},
                {"value": "Moderate"},
                {"value": "Major"},
                {"value": "Critical"}
            ]
        }
    },
    {
        "referenceName": "Custom.Priority",
        "name": "Priority",
        "type": "String",
        "pickList": {
            "items": [
                {"value": "Low"},
                {"value": "Medium"},
                {"value": "High"}
            ]
        }
    }
]

# Create custom fields by updating the work item type
for custom_field in custom_fields:
    url = f"https://dev.azure.com/{organization}/{project}/_apis/work/processes/{process_id}/workitemtypes/Bug?api-version=7.0-preview.1"

    headers = {
        "Content-Type": "application/json-patch+json",
        "Authorization": f"Basic {pat}"
    }

    # Define the request body for updating the work item type
    request_body = [
        {
            "op": "add",
            "path": "/fields/-",
            "value": custom_field
        }
    ]

    # Send the PATCH request
    response = requests.patch(url, headers=headers, json=request_body)

    if response.status_code == 204:
        print(f"Custom field '{custom_field['name']}' added successfully.")
    else:
        print(f"Failed to add custom field '{custom_field['name']}': {response.status_code} - {response.text}")