Skip to main content

How to Enforce Unique Values in a SharePoint Column with the REST API in Power Automate

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A unique column refuses a value that another item already has: SharePoint answers This value already exists in the list. A flow can make a new column unique as it creates it, or make an existing column, such as Title, unique afterwards.

Syntax​

Both requests go through Send an HTTP request to SharePoint (How to Use Send an HTTP Request to SharePoint). A unique column must also be indexed: Microsoft's Create list relationships by using lookup columns says a unique column must also have an index. So both requests set the two together.

A new column​

Add two attributes to the column's Field element and create it with createfieldasxml (How to Add a Column to a SharePoint List with CreateFieldAsXml):

SchemaXml: general form
<Field Type='<type>' Name='<internal name>' StaticName='<internal name>' DisplayName='<display name>' Indexed='TRUE' EnforceUniqueValues='TRUE' />
  • Indexed='TRUE': in Microsoft's Field element reference, TRUE if the column is indexed.
  • EnforceUniqueValues='TRUE': the column refuses duplicates.
  • <type>: a type that can be unique. Microsoft's article lists single line of text (Text), choice with one value, number, currency, date and time, lookup with one value and person with one value. Multiple lines of text, Yes/No and the multi-value types can't be unique. The other attributes are the column's usual ones.

An existing column​

Change the column with a MERGE request:

FieldValue
MethodPOST
Uri_api/web/lists/getbytitle('<list name>')/fields/getbyinternalnameortitle('<column>')
HeadersAccept: application/json;odata=nometadata
Content-Type: application/json;odata=verbose
IF-MATCH: *
X-HTTP-Method: MERGE
Body: general form
{
"__metadata": { "type": "SP.Field" },
"Indexed": true,
"EnforceUniqueValues": true
}
  • <list name> and <column>: the list's title, and the column's internal name or display name.
  • X-HTTP-Method: MERGE: the request changes only the properties in the body. Microsoft's REST overview says that with MERGE, any properties that you don't explicitly set keep their current property.
  • IF-MATCH: *: change the column whatever its current version.
  • Indexed and EnforceUniqueValues: the same two settings, as JSON true, without quotes. Both are properties you can set on a column, in Microsoft's Field.Indexed and Field.EnforceUniqueValues reference.

The Example​

A blank list called Rooms holds meeting rooms. The flow, Make Values Unique, does both:

  • Add RoomCode adds a required, unique RoomCode column for the code on each room's door;
  • Make Title unique makes the list's Title column, the room's name, unique too.

Prerequisites​

  • Permission to change the list, such as Edit or Full Control on the site, for the account the flow's SharePoint connection uses.
  • No duplicates in an existing column you make unique. Remove them first.

Step 1: Add a Unique Column​

In a flow with Manually trigger a flow, add Send an HTTP request to SharePoint, rename it Add RoomCode, and fill it in:

FieldValue
Site AddressYour site
MethodPOST
Uri_api/web/lists/getbytitle('Rooms')/fields/createfieldasxml
HeadersAccept: application/json;odata=nometadata
Content-Type: application/json;odata=verbose
Add RoomCode → Body
{
"parameters": {
"__metadata": { "type": "SP.XmlSchemaFieldCreationInformation" },
"SchemaXml": "<Field Type='Text' Name='RoomCode' StaticName='RoomCode' DisplayName='RoomCode' Description='The code on the room door, such as B204. Each code can appear only once.' Required='TRUE' MaxLength='20' Indexed='TRUE' EnforceUniqueValues='TRUE' />",
"Options": 25
}
}

The Add RoomCode action with Method POST, the Uri ending in getbytitle('Rooms')/fields/createfieldasxml, the two headers, and the Body whose SchemaXml has Type Text, Name RoomCode, Required TRUE, MaxLength 20, Indexed TRUE and EnforceUniqueValues TRUE

Step 2: Make an Existing Column Unique​

Add a second Send an HTTP request to SharePoint, Make Title unique:

FieldValue
Site AddressYour site
MethodPOST
Uri_api/web/lists/getbytitle('Rooms')/fields/getbyinternalnameortitle('Title')
HeadersAccept: application/json;odata=nometadata
Content-Type: application/json;odata=verbose
IF-MATCH: *
X-HTTP-Method: MERGE
Make Title unique → Body
{
"__metadata": { "type": "SP.Field" },
"Indexed": true,
"EnforceUniqueValues": true
}

The Make Title unique action with Method POST, the Uri ending in fields/getbyinternalnameortitle('Title'), the headers Accept, Content-Type, IF-MATCH * and X-HTTP-Method MERGE, and the Body with __metadata type SP.Field, Indexed true and EnforceUniqueValues true

Save the flow and test it manually (How to Test a Cloud Flow Manually in Power Automate). Both actions succeeded.

The Result​

I added a room in the list, Board room with the code B204. Then I added another, Quiet room, and typed the same code. SharePoint refused to save it:

The New item form with the message Error: This value already exists in the list. at the top, Title Quiet room, and RoomCode B204 with This value already exists in the list. under it, above the description The code on the room door, such as B204. Each code can appear only once.

A different code saves as usual. Title stays optional on this list, as it was: the request changed only the two properties in its body.

Common Mistakes​

MistakeWhat happensDo this instead
Setting EnforceUniqueValues aloneMicrosoft says a unique column must also have an indexSet Indexed in the same request
"true" in quotes in the MERGE bodyIt's text, not a JSON trueWrite true without quotes
Leaving out X-HTTP-Method: MERGEThe POST isn't sent as an update of the columnAdd the header, with IF-MATCH: *
Choosing a Yes/No or multi-person columnMicrosoft lists these types as unsupported for unique valuesUse a supported type, such as single line of text

Troubleshooting​

SymptomLikely causeFix
Status 404 on the MERGENo column with that name in that listCheck the column's internal name
Status 403The connection's account can't change the listGive it Edit or Full Control
This value already exists in the list.Another item has that valueUse another value, or change the other item
The change is blocked on a big listMicrosoft says making a column unique may be blocked when the list has more items than the List View ThresholdMake the column unique while the list is small

FAQ​

Does uniqueness ignore capital letters?​

Yes. Microsoft's article says uniqueness is not case-sensitive: ORD-231 and ord-231 count as the same value. The REST request turns on the same setting as the column's Enforce unique values switch in the browser.

How do I let duplicates in again?​

EnforceUniqueValues is a property you can set either way, so the same MERGE with false is the way back; the example didn't need it. Microsoft's article adds that the index can't be removed unless you first redefine the column to allow duplicate values.

Does this need a premium licence?​

No. Send an HTTP request to SharePoint is part of the standard SharePoint connector.

Conclusion​

For a new column, add Indexed='TRUE' and EnforceUniqueValues='TRUE' to its Field element. For an existing one, send a MERGE to fields/getbyinternalnameortitle('<column>') with "Indexed": true and "EnforceUniqueValues": true. Either way, a second item with the same value gets This value already exists in the list.

Additional Resources​