Friday, May 1, 2015

Cloud Business App: Save Operation Failed. The etag value in the request header does not match with the current etag value of the object

In Cloud Business App, this issue occurred when trying to update the record using SharePoint 2013 custom list data source from View Announcement HTML client screen. When I click on Save button in Edit form. The cloud business app is developed using Visual Studio 2013.

To overcome this issue add following java script in HTML Client Project >> Default.htm page inside the <Body> tag.

<script type="text/javascript">
var origMetadataReadFunc = OData.metadataHandler.read;
OData.metadataHandler.read = function (response, context) {
origMetadataReadFunc.call(OData.metadataHandler, response, context);
var data = response.data,
schema = data && data.dataServices && data.dataServices.schema && data.dataServices.schema[0],
entities = schema && schema.entityType || [];
entities.forEach(function (entity) {
var i,
property,
properties = entity.property || [];
for (i = properties.length - 1; i >= 0; i--) {
property = properties[i];
if (property.name === "Microsoft_LightSwitch_ETag") {
property.type = "Edm.String";
break;
}
}
});
};
var origJsonReadFunc = OData.jsonHandler.read;
OData.jsonHandler.read = function (response, context) {
var result = origJsonReadFunc.call(OData.jsonHandler, response, context);
var data = response.data, results = data.results;
if (results) {
results.forEach(function (entity) {
if (entity.__metadata.etag) {
var etag = entity.__metadata.etag,
firstIndex = etag.indexOf("'"),
lastIndex = etag.lastIndexOf("'"),
coreEtag = "";
for (var i = firstIndex + 1; i < lastIndex; i++) {
var char = etag[i];
coreEtag += char;
if (char == "'") {
coreEtag += "'";
if (etag[i + 1] == "'") {
i++;
}
}
}
entity.__metadata.etag = etag.substr(0, firstIndex + 1) + coreEtag + etag.substr(lastIndex);
}
});
}
return result;
};
</script>
Enjoy SharePoint!!!

No comments:

Post a Comment