Gotcha using Office 365 / SharePoint REST API and ODATA Minimal Metadata


You’ve probably heard that in Office 365 / SharePoint Online, the REST API service now includes support for JSON light responses, meaning that you can vary the style of the ODATA response.

Reading that post you’ll notice that the ‘shape’ of the response is also different when using the minimalmetadata or nometadata types;

For example, when using ODATA=verbose, your response will look like this;

o365restodata-03

And when using ODATA=minimalmetadata or ODATA=nometadata, your response will look like this;

o365restodata-04

In the former, your data is returned in the response.d.results property, and in the later it’s returned in the response.value property.

While thats all great, what the post doesn’t mention is that when using ODATA=minimalmetadata or ODATA=nometadata, the ‘shape’ of failure responses is also changed.

So assuming my REST request, includes a field which doesn’t exist (an easy example) and I’m using ODATA=verbose, your error response will look like this;

o365restodata-01

If I’m using ODATA=minimalmetadata or ODATA=nometadata, your error response will look like this;

o365restodata-02

In the former, the error object is in a property named error and in the later it’s in a property named odata.error.

Everything else is the same, though to access the error object you have to use square bracket notation instead of dot notation, here’s a GIST demonstrating;

news.popular.getPosts = function() {
var
p = new $.Deferred(),
r = {
url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Posts')/items?$select=Id,Title,PublishedDate,LikesCount,NumCommentsId,PostCategoryId&$orderby=PublishedDate desc"
+ getFilter(),
type: 'GET',
headers: { ACCEPT: 'application/json;odata=minimalmetadata' }
};
$.ajax(r)
.done(function(response) {
var data = (response.value || response.d.results || response.d);
var populardata = [];
$.each(data, function(i,e) {
if (e.LikesCount || e.NumCommentsId) {
populardata.push(e);
}
});
p.resolve(populardata);
})
.fail(function (xhrObj, textStatus, err) {
var e = JSON.parse(xhrObj.responseText),
err = e.error || e["odata.error"],
m = '<div style="color:red;font-size:1.2em;">Exception<br/>&raquo; ' +
((err && err.message && err.message.value) ? err.message.value : (xhrObj.status + ' ' + xhrObj.statusText))
+'</div>';
p.reject({ success: false, error: m, uri: r.url });
});
return p.promise();
}

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

One thought on “Gotcha using Office 365 / SharePoint REST API and ODATA Minimal Metadata

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.