cancel
Showing results for 
Search instead for 
Did you mean: 

How to use $search when calling external service through destination in SAP CAP NodeJS?

boolan
Participant
0 Kudos

Hi,

I am writing an SAP CAP application on NodeJS that requests data from MS Graph.

I have defined a destination in SAP BTP (name: "Graph").

I have created a js service file with the following method to read a particular user's data (user id: "ABCDE"):

const readFunction = async (req) => {

let destination = 'Graph'

const url = `/users/?$search="onPremisesSamAccountName:ABCDE"`

console.log('In function readFunction..');

console.log('Trying to get to destination..');

const service = await cdsapi.connect.to(destination);

console.log('Connected to destination..');

const graphUser = await service.run({

url: url,

headers: { "ConsistencyLevel": "Eventual"}

})

console.log('Got user data..');

let users = graphUser.value.map(graph_user => {

var user = {}

user.aad_id = graph_user.id

user.username = graph_user.userPrincipalName

user.displayName = graph_user.displayName

user.givenName = graph_user.givenName

user.surname = graph_user.surname

user.isAzureActiveDirectory = true

return user

})

return users

}

module.exports = (srv) => {

srv.on('READ', 'User', readFunction)

}

But when I call this service, I get the following error in the console:

error: {

code: 'Request_UnsupportedQuery',

message: "Request with $search query parameter only works through MSGraph with a special request header: 'ConsistencyLevel: eventual'", (...)

As you can see, I have provided the ConsistencyLevel parameter on the request header, but it does not work.

I do not see it in the request header in the Chrome dev tools.

I have also added the property URL.headers.ConsistencyLevel to destination properties in SAP BTP, but it does not work either.

My questions are:

How can I correctly pass the $search parameter to the destination?

How can I set the request header values in SAP CAP on NodeJS?

When I take the $search parameter out from the path (url = `/users`),

the request works and delivers me all the users from MS Graph.

Accepted Solutions (1)

Accepted Solutions (1)

Hi,

assuming that you're using `@sapmentors/cds-scp-api` npm package in order to connect to the MS Graph via destination, any header passed to the `service.run` method will be ignored.

There's an open issue regarding this problem in the project repository: headers data passed in from client app gets removed in function getAuthorizationHeader. The fix was merged on Jan 3rd, 2022 (Merge pull request #5 from EdoB/fixRequestHeadersDeletion) but it hasn't been officially released to the npm registry yet.

Without that defect, it's possible to pass the required header and you should get the appropriate data from MS Graph just fine.

Answers (0)