Edit

Share via


Update and delete table rows by using the Web API

Operations that modify data are a core part of the Web API. In addition to simple update and delete operations, you can perform operations on single table columns (entity attributes) and compose upsert requests that either update or insert data depending on whether it exists.

Basic update

Update operations use the HTTP PATCH verb. Pass a JSON object containing the properties you want to update to the URI that represents the record. If the update is successful, the response returns a status of 204 No Content.

The If-Match: * header ensures you don't create a new record by accidentally performing an upsert operation. For more information, see Prevent create in upsert.

Important

When updating an entity, only include the properties you are changing in the request body. If you update an entity by including all the properties of an entity that you previously retrieved, the operation updates each property even if the value is the same. This update can cause system events that trigger business logic that expects that the values have changed. It can cause properties to appear to be updated in auditing data when they didn't actually change.

When you update the statecode property, always set the desired statuscode. The statecode and statuscode values depend on each other. For a given statecode value, there can be multiple valid statuscode values. However, each statecode column has a single DefaultStatus value configured. When you update statecode without specifying a statuscode, the system sets the default status value. Also, if you enable auditing on the table and the statuscode column, the changed value for the statuscode column isn't captured in the audit data unless you specify it in the update operation.

Note

The definition for attributes includes a RequiredLevel property. When this property is set to SystemRequired, you can't set these attributes to a null value. For more information, see Attribute requirement level.

This example updates an existing account record with the accountid value of 00000000-0000-0000-0000-000000000001.

Request:

PATCH [Organization URI]/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001) HTTP/1.1  
Content-Type: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0
If-Match: *  
  
{  
    "name": "Updated Sample Account ",  
    "creditonhold": true,  
    "address1_latitude": 47.639583,  
    "description": "This is the updated description of the sample account",  
    "revenue": 6000000,  
    "accountcategorycode": 2  
}  

Response:

HTTP/1.1 204 No Content  
OData-Version: 4.0  
  

Note

For information about associating and disassociating entities on update, see Using single-valued navigation properties.

Update with data returned

To retrieve data from an entity you're updating, compose your PATCH request so that it returns data from the updated record with a status of 200 (OK). To get this result, use the Prefer: return=representation request header.

To control which properties are returned, append the $select query option to the URL for the entity set. The $expand query option is ignored if used.

This example updates an account entity and returns the requested data in the response.

Request:

PATCH [Organization URI]/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001)?$select=name,creditonhold,address1_latitude,description,revenue,accountcategorycode,createdon HTTP/1.1  
OData-MaxVersion: 4.0  
OData-Version: 4.0  
Accept: application/json  
Content-Type: application/json; charset=utf-8  
Prefer: return=representation
If-Match: * 
  
{"name":"Updated Sample Account"}  

Response:

HTTP/1.1 200 OK  
Content-Type: application/json; odata.metadata=minimal  
Preference-Applied: return=representation  
OData-Version: 4.0  
  
{  
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts/$entity",  
    "@odata.etag": "W/\"536537\"",  
    "accountid": "00000000-0000-0000-0000-000000000001",  
    "accountcategorycode": 1,  
    "description": "This is the description of the sample account",  
    "address1_latitude": 47.63958,  
    "creditonhold": false,  
    "name": "Updated Sample Account",  
    "createdon": "2016-09-28T23:14:00Z",  
    "revenue": 5000000.0000,  
    "_transactioncurrencyid_value": "048dddaa-6f7f-e611-80d3-00155db5e0b6"  
}  
  

Update multiple records in a single request

The fastest way to update multiple records of the same type in a single request is to use the UpdateMultiple action. Not all standard tables support this action, but all elastic tables do.

More information:

Update a single property value

To update a single property value, use a PUT request and add the property name to the entity's Uri.

The following example updates the name property of an existing account row with the accountid value of 00000000-0000-0000-0000-000000000001.

Request:

PUT [Organization URI]/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001)/name HTTP/1.1  
Content-Type: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0  
  
{"value": "Updated Sample Account Name"}  

Response:

HTTP/1.1 204 No Content  
OData-Version: 4.0  
  

Delete a single property value

To delete the value of a single property, use a DELETE request with the property name appended to the URI of the entity.

The following example deletes the value of the description property of an account entity with the accountid value of 00000000-0000-0000-0000-000000000001.

Request:

DELETE [Organization URI]/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001)/description HTTP/1.1  
Content-Type: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0  

Response:

HTTP/1.1 204 No Content  
OData-Version: 4.0  
  

Note

You can't use this approach with a single-valued navigation property to disassociate two entities. For an alternative approach, see Disassociate by using a single-valued navigation property.

Upsert a table row

An upsert operation is similar to an update. It uses a PATCH request and uses a URI to reference a specific record. The difference is that if the record doesn't exist, it's created. If it already exists, it's updated.

Upsert is valuable when synchronizing data between external systems. The external system might not contain a reference to the primary key of the Dataverse table, so you can configure alternate keys for the Dataverse table by using values from the external system that uniquely identify the record on both systems. More information: Define alternate keys to reference rows

You can see any alternate keys that are defined for a table in the annotations for the entity type in the $metadata service document. More information: Alternate Keys.

In the following example, there's a table with the name sample_thing that has an alternate key that refers to two columns: sample_key1 and sample_key2, which are both defined to store integer values.

Request:

PATCH [Organization URI]/api/data/v9.2/sample_things(sample_key1=1,sample_key2=1) HTTP/1.1
Accept: application/json 
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Content-Type: application/json

{
    "sample_name": "1:1"
}

For both create or update operations, you get the same response. Notice how the OData-EntityId response header uses the key values rather than the GUID primary key identifier for the record.

Response:

HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: [Organization URI]/api/data/v9.2/sample_things(sample_key1=1,sample_key2=1)

Because the response is the same, you can't know whether the operation represented a Create or Update operation.

If you need to know, you can use the Prefer: return=representation request header. By using this header, you get a 201 Created response when a record is created and a 200 OK response when the record is updated. This option adds a Retrieve operation, which has an impact on performance. If you use the Prefer: return=representation request header, make sure that your $select includes the minimal amount of data, preferably only the primary key column. More information: Update with data returned and Create with data returned.

When using alternate keys, don't include the alternate key values in the body of the request.

  • When an upsert represents an Update, these alternate key values are ignored. You can't update alternate key values while using them to identify the record.
  • When an upsert represents a Create, the key values in the URL are set for the record if they aren't present in the body. So there's no need to include them in the body of the request.

More information: Use Upsert to Create or Update a record

Note

Normally when creating a new record you let the system assign a GUID value for the primary key. This practice is best because the system generates keys that are optimized for the index and this choice improves performance. But if you need to create a record with a specific primary key value, such as when the key GUID value is generated by an external system, the upsert operation provides a way to do this.

Prevent create or update with upsert

Sometimes, you want to perform an upsert but prevent one of the potential operations: either create or update. You can prevent these operations by using the If-Match or If-None-Match headers. For more information, see Limit upsert operations.

Basic delete

A delete operation is straightforward. Use the DELETE verb with the URI of the entity you want to delete. This example message deletes an account entity with the primary key accountid value equal to 00000000-0000-0000-0000-000000000001.

Request:

DELETE [Organization URI]/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001) HTTP/1.1  
Content-Type: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0  

Response:

If the entity exists, you get a normal response with status 204 to indicate the delete was successful. If the entity isn't found, you get a response with status 404.

HTTP/1.1 204 No Content  
OData-Version: 4.0  

Check for duplicate records

For more information on how to check for duplicate records during an update operation, see Detect duplicates during Update operation using the Web API.

Delete multiple records in a single request

To delete multiple records of the same type in a single request, use the DeleteMultiple action. Standard tables don't support this the DeleteMultiple action, but all elastic tables do.

Note

For standard tables, use the BulkDelete action. This action enables asynchronous deletion of records that match a query. For more information, see Delete data in bulk.

More information:

Update and delete documents in storage partitions

If you're updating or deleting elastic table data stored in partitions, specify the partition key when you access that data.

More information: Choosing a PartitionId value

See also

Web API Basic Operations Sample (C#)
Web API Basic Operations Sample (Client-side JavaScript)
Perform operations using the Web API
Compose Http requests and handle errors
Query Data using the Web API
Create a table row using the Web API
Retrieve a table row using the Web API
Associate and disassociate table rows using the Web API
Use Web API functions
Use Web API actions
Execute batch operations using the Web API
Impersonate another user using the Web API
Perform conditional operations using the Web API