Using the GRAPH API
- OPTION 1 ( Best & simplest option )
You can query the graph endpoint:
https://graph.microsoft.com/v1.0/me/insights/used?$filter=ResourceVisualization/Type eq spsite'&$orderby=lastUsed/lastAccessedDateTime desc
It will return accurate information of the latest visited sites.
The recently Visited Sites webpart from the sharepoint sp-starter-kit, already shows the latest visited sites by the current user with the help of graph api.
This webpart gets the latest web pages the user has visited, on the endpoint:
https://graph.microsoft.com/v1.0/me/insights/used?$filter=ResourceVisualization/containerType eq 'Site' and ResourceVisualization/Type eq 'Web'
Then, builds an array of unique site url based on the results returned by the query.
Please notice that option 2's results are always based on visited page items or files, then, if a user only visits the site homepage, such result wont be reflected in the query answer.
Using Sharepoint REST API
If you are looking for an alternative way of retrieving the latest visited sites, you can query the below endpoint ( replace top with number of desired results )
/_vti_bin/homeapi.ashx/sites/recent??mostRecentFirst=true&start=0&count=${top}&acronyms=true
And dont forget to add the additional header SPHome-ClientType
.
Here is a javascript example:
return await (await this.httpCli.get(
`/_vti_bin/homeapi.ashx/sites/recent??mostRecentFirst=true&start=0&count=${top}&acronyms=true`,
SPHttpClient.configurations.v1,
{ headers: { "SPHome-ClientType": "SharePointIOS" } })
).json();
PS1: feel free to test the microsoft graph api here
PS2: This sharepoint rest api will be exposed and documented some day in graph api, as described on this github thread.