{"error":{"code":"BadRequest","message":"Resource not found for the segment 'messageTrace'.","innerError":{"date":"2023-04-07T21:38:11"

Alexis Crawford 201 Reputation points
2023-04-07T23:04:30.9+00:00

Hello, When run the code to fetch total item count for all users in the company, I get the following error: {"error":{"code":"BadRequest","message":"Resource not found for the segment 'messageTrace'.","innerError":{"date":"2023-04-07T21:38:11" "request-id":"2c100245-3b1a-44a2-b0a6-e515df75d898","client-request-id":"2c100245-3b1a-44a2-b0a6-e515df75d898"}}}. I am expecting a list of users with their item count in their mailbox. Can someone help me find the correct query syntax please? The query is :

var client = new ConfidentialClientApplication(config);

var request = {
  scopes: ["https://graph.microsoft.com/.default"],
};

let response = await client.acquireTokenByClientCredential(request);

console.dir(response);

let startDateTime = new Date(
  new Date().setDate(new Date().getDate() - 7)
).toISOString(); // Set the start time to 7 days ago
let endDateTime = new Date().toISOString(); // Set the end time to now
let query = await fetch(
  `https://graph.microsoft.com/v1.0/auditLogs/messageTrace?$filter=SentDateTime ge ${startDateTime}Z and (SentDateTime)lt ${endDateTime}Z`,
  {
    headers: {
      Authorization: `Bearer ${response.accessToken}`,
    },
  }
);
let rawResponse = await query.text(); // get the raw response as text
console.log(rawResponse); // log the response to inspect it in the console

let data = null;
try {
  data = JSON.parse(rawResponse.trim()); // parse the response as JSON
} catch (err) {
  console.error("Error parsing JSON:", err.message);
}


Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,782 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
897 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vasil Michev 96,436 Reputation points MVP
    2023-04-08T07:51:11.65+00:00

    Not sure where you're getting this code from, but there is no /auditLogs/messageTrace endpoint available within the Graph API, at least not a public one. The only way to get this data would be via PowerShell and the Get-MessageTrace cmdlet. Moreover, this does not give you "total item count" for mailboxes, if that's what you are after use the /users/{userId}/messages/$count endpoint.

    0 comments No comments