Sorry for the radio silence lately. Reason behind this is that I either haven’t been working on anything interesting or the work has been so intense that I didn’t have any time at all to post anything.

After quite a while I’ve again started developing Office 365 applications using SPFX. Love the framework and how much it advanced in the past 2 years. I’ll keep this one short – I’ve been building an Org Chart web part and got stuck doing a REST call to a SharePoint Online list. I have been using thisas a guide. I wanted returned data to be as ‘clean’ as possible and therefor tried to use the following Accept header: ‘Accept’:’application/json;odata=nometadata;’ so the REST call only returns data I need. More on odata in the Accept header here and here. Using this Accept header would result in “The HTTP header ACCEPT is missing or its value is invalid” error. The strange thing was that is was working in Postman perfectly. This was the code I have been using to do a batch request:

const userResponses: Promise[] = [];  
const opt: ISPHttpClientBatchOptions = { 
    headers: { 'Accept':'application/json;odata=nometadata;'} 
}; 
const getUsers: Promise = spBatch.get(
    `${this._currentWebUrl}/_api/web/Lists/GetByTitle('${encodeURIComponent(list)}')/items? 
    $select=User/Id,User/Title,User/Name,User/EMail,Manager/Title&$expand=User,Manager`
    ,SPHttpClientBatch.configurations.v1, opt);  
userResponses.push(getUsers);

I was made aware by Gautam Sheth that I have been using OData V3 syntax in my code and that SPFX uses OData V4. Apparently there are two ways to fix this error – either by using OData V4 syntax in the header:

const userResponses: Promise[] = [];  
const opt: ISPHttpClientBatchOptions = { 
    headers: { 'Accept':'application/json;odata.metadata=minimal'} 
}; 
const getUsers: Promise = spBatch.get(
    `${this._currentWebUrl}/_api/web/Lists/GetByTitle('${encodeURIComponent(list)}')/items? 
    $select=User/Id,User/Title,User/Name,User/EMail,Manager/Title&$expand=User,Manager`
    ,SPHttpClientBatch.configurations.v1, opt);  
userResponses.push(getUsers);

Or by sticking to OData V3 and specifying that explicitly in the header. Unfortunately, I haven’t been able to test the latter.

Differences between V3 and V4 syntax:

V3 V4
odata=nometadata odata.metadata=none
odata=minimalmetadata odata.metadata=minimal
odata=verbose odata.metadata=full

Hope this helps!

Tags ___ , , ,