Tuesday, February 16, 2016

Handling ETag value on SharePoint REST API Updates

ETag value is a part of the HTTP protocol that allows concurrency control and web cache validation. ETag is a broader topic when considered in general, but the main focus on this article is to discuss about REST API usage within SharePoint. 

ETag value is used whenever we make a POST request to a REST API endpoint. This is handled through the IF-Match request header. A sample request is displayed below.

So now lets see how this header works.

Once we make a GET request, we receive an ETag value as a part of the meta data as you can see in the image below. 

This is a numeric value and it represent the resource version of the resource at the time the GET request is made. Given these data can be accessed by many users within the same time, there can be a difference of the data by the time a user attempts to update. (Simple concurrency issue) 

With ETag value we can check whether the values do match each other by the time the update operation is performed. If the values match, an update will be performed. If not, an error will be returned as follows.

How to Handle this?

As I mentioned this earlier, we receive the ETag value once we make a GET request. So at the point of we receive it we can store that in a hidden field. At the point of update we can assign it to the IF-Match header. 

If we get the exception returned then we can prompt the user to perform the update with the latest information.

How to Ignore?

Ignoring is simple. You can simply set the value to "*" which will ignore the ETag value and perform the update. This is shown in the first image.

No comments:

Post a Comment