Laurent
Laurent

Reputation: 3

Create fiels list with rest Api in azure devops

I want to create a field list in azure devps with rest API
I follow the following documentation

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/fields/create?view=azure-devops-rest-7.1&tabs=HTTP#fieldtype

but we have no example with a list of strings. In field type we must set string to list

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/fields/create?view=azure-devops-rest-7.1&tabs=HTTP#fieldtype

but I have no idea to set list. In IHM I see my field but it's type of string.

Here my json request:

{   "name": "MyField",
    "referenceName": "Custom.MyField",
    "description": "custom fieldcreate with API",
    "type": "string",
    "usage": "workItem",
    "readOnly": false,
    "canSortBy": true,
    "isQueryable": true,
    "supportedOperations": [
      {
        "referenceName": "SupportedOperations.Equals",
        "name": "="
      }
    ],
    "isIdentity": true,
    "isPicklist": true,
    "isPicklistSuggested": false,
    "url": null } 

Any Idea to do that?

I also try picklist but I don't think that is the solution:

{
  "id": null,
  "name": "picklist_aef2c045-0d2d-4f92-9d09-56eea553e1ef",
  "type": "String",
  "url": null,
  "items": [
     "Blue",
     "Green",
     "Red"
   ],
  "isSuggested": false
  }

Upvotes: 0

Views: 78

Answers (1)

Miao Tian-MSFT
Miao Tian-MSFT

Reputation: 5642

To create a field list in Azure DevOps using the REST API, first, you need to create a picklist with Lists - Create API.

After creating the picklist, you will receive a response containing the id of the picklist. You can use this ID to create the picklist in your custom field with Fields - Create API.

Here is a sample PowerShell script:

# Define variables
$organization = ""
$project = ""
$pat = ""

# Base64 encode the PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

# JSON body for creating the picklist
$picklistJsonBody = @"
{
    "name": "MyTestPicklist",
    "type": "String",
    "items": ["Yellow", "Green", "Red"]
}
"@

# Create the picklist
$picklistResponse = Invoke-RestMethod -Uri "https://dev.azure.com/$organization/_apis/work/processes/lists?api-version=7.1" `
    -Method Post `
    -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} `
    -Body $picklistJsonBody `
    -ContentType "application/json"

$picklistId = $picklistResponse.id

# JSON body for creating the custom field
$fieldJsonBody = @"
{
    "name": "MyNewTestField",
    "referenceName": "Custom.MyNewTestField",
    "description": "Custom field created with API",
    "type": "string",
    "usage": "workItem",
    "readOnly": false,
    "canSortBy": true,
    "isQueryable": true,
    "supportedOperations": [
        {
            "referenceName": "SupportedOperations.Equals",
            "name": "="
        }
    ],
    "isPicklist": true,
    "picklistId": "$picklistId"
}
"@

# Create the custom field
Invoke-RestMethod -Uri "https://dev.azure.com/$organization/$project/_apis/wit/fields?api-version=7.1" `
    -Method Post `
    -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} `
    -Body $fieldJsonBody `
    -ContentType "application/json"


Upvotes: 0

Related Questions