Flow: Create SharePoint List Choice Fields Using REST API with Power Automate
When you have an existing SharePoint list or if you have created one using the method described in Create SharePoint List REST API with Power Automate, you will then want to populate it with some fields.
There are several FieldTypeKinds, at least 31 and you kind find more information on them at this link, FieldType Members.
In this post, I will cover another commonly used FieldType in SharePoint, the "choice" field.
Here is the complete Flow:
NOTE: There are 2 REST POST calls we will make. 1 that creates the field, one that adds the new field to the default view. Using REST, when you create the new field there is still a requirement to add it to a view. For the purpose of demonstration, we will use the default "All Items" view but you amend to suit if you have a need to do so.
As detailed above, start with a button and "Manually trigger a Flow", unless of course you want to adapt this and use a recurrence, HTTP POST or whatever you suits your use case.
Next, I am initialising 2 variables that include the SharePoint Site URL and one for the list name. Again, not required, you could just add them to them in to the "Send an HTTP request to SharePoint" action but this is a little neater and can help for changes in the future.
Would you like to buy Alan a coffee?
Visit the AlanPs1 Ko-fi page
The Compose below is the name of the choice field we are looking to create.
Now for the all important REST POST calls, starting with "Send an HTTP request to SharePoint Create List Columns Choice".
Url
_api/web/Lists/getByTitle('@{variables('ListTitle')}')/Fields
Headers
Accept | application/json;odata=verbose
Content-Type | application/json;odata=verbose
Body
{
"__metadata": {
"type": "SP.FieldChoice"
},
"FieldTypeKind": 6,
"Title": "@{outputs('Compose_Columns_Choice_NPSStatus')}",
"Required": "false",
"EnforceUniqueValues": "false",
"StaticName": "@{outputs('Compose_Columns_Choice_NPSStatus')}",
"Choices": { 'results': ['Detractor', 'Passive', 'Promoter'] }
}
Next, let's create "Send an HTTP request to SharePoint Add Column To View".
Url
_api/web/Lists/getByTitle('@{variables('ListTitle')}')/views/GetByTitle('All%20Items')/ViewFields/AddViewField('@{outputs('Compose_Columns_Choice_NPSStatus')}')
Headers
Accept | application/json;odata=verbose
Content-Type | application/json;odata=verbose
You will now have created a field in the SharePoint list called "HeadwindNPS", see below for an example that shows it is in the list and also a feature of the default view called "All Items"
Thanks for reading, AlanPs1
Visit the Get Help section of the Power Automate Community