Question

Sharepoint Online offers a view of the frequent sites of the current user. It is available in the home page of the "My Site" of the user. I'm asking if there is a REST API (or something like) to get the FREQUENT SITES of the user programmatically? Maybe also a Graph API query?

frequent sites

I know there is a way to get the LAST VISITED sites of the user but I don't mean that

/_api/search/query?querytext='contentclass:STS_Web'&sortlist='ViewsRecent:descending,ViewsLifeTime:descending'&selectproperties='Title,Author,Url'

And there is a way to get the FOLLOWED SITES but I don't mean neither that

/_api/social.following/my/followed(types=4)

Thank you

PS: I found the results in this call (sniffing the network calls of the page) but the host is strange...): https://northeurope1-sphomep.svc.ms/api/v1/sites/feed?acronyms=true&start=0&count=12

Was it helpful?

Solution

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.

  • OPTION 2

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top