Monday, October 6, 2014

Get a list of the current user’s recent documents

When navigating to OneDrive in SharePoint Online you get a nice list on the left showing your recent documents. The list show documents you have recently used, that being opened/viewed or stored. I suspect this is sort of overlapping with My work in Delve, but different API’s and results.

image

A recent discussion internally about bringing relevant content to you on your start page, made me dig into this. Put it as part of your intranet home page, or filter the site on any project site, to show your most recent files. It’s all about you!

So, how do you get this list of documents? By using a REST call to the end-point

https://<tenant>.sharepoint.com/_api/me/getrecentdocs

If you add the usual headers of application/json;odata=verbose, you get the data back as JSON. If not, you get it as XML. For me it returned 31 items, more than enough for a good listing.

10 comments:

  1. Sucks that the returned results doesn't include the OWA url. Need to manually create this.. and that's complicated!

    ReplyDelete
    Replies
    1. Easiest is to search up all the documents with a path: property query and use the ServerRedirectURL managed property. This property contains a link to WAC :)

      Delete
  2. Hi Can you Plz Provide me sample Code or more detail which webpart you have used to achieve this Bcoz i have Same Requirement in my Project and I am trying to achieve this using Content Search but did`nt get by USER result

    ReplyDelete
  3. The api is an undocumented api and stopped working 17/10 for some reason. I talked to the support and they said that is was not supported...

    ReplyDelete
    Replies
    1. Seems this has been replaced with the following search query instead:
      https://.sharepoint.com/_api/search/query?querytext='*'&querytemplate='(AuthorOwsUser:{User.AccountName} OR EditorOwsUser:{User.AccountName}) AND ContentType:Document AND IsDocument:1 AND -Title:OneNote_DeletedPages AND -Title:OneNote_RecycleBin NOT(FileExtension:mht OR FileExtension:aspx OR FileExtension:html OR FileExtension:htm)'&rowlimit=50&bypassresulttypes=false&selectproperties='Title,Path,Filename,FileExtension,Created,Author,LastModifiedTime,ModifiedBy,LinkingUrl,SiteTitle,ParentLink,DocumentPreviewMetadata,ListID,ListItemID,SPSiteURL,SiteID,WebId,UniqueID,SPWebUrl'&sortlist='LastModifiedTime:descending'&enablesorting=true

      Delete
    2. Nice!

      I did another "similar" one by adding a graph query.


      /_api/search/query?querytext=%27(*)+AND+(FileExtension:doc+OR+FileExtension:docx+OR+FileExtension:ppt+OR+FileExtension:pptx+OR+FileExtension:xls+OR+FileExtension:xlsx+OR+FileExtension:xlsm+OR+FileExtension:pdf)%27&selectproperties=%27Title%2c+FileExtension%2c+ParentLink%2c+ServerRedirectedURL%2c+SPWebUrl%2c+SiteTitle%27&rowlimit=5&rankingmodelid=%270c77ded8-c3ef-466d-929d-905670ea1d72%27&clienttype=%27ContentSearchRegular%27&properties=%27GraphQuery:ACTOR(ME\\,%20OR(action\\:1001\\,action\\:1003)),GraphRankingModel:{%22features%22\\:[{%22function%22\\:%22EdgeTime%22}]}%27

      Delete
  4. Beware that this functionality only works on SharePoint Online (and probably with SharePoint 2016 hybrid in the future). The suggested solution uses Office Graph, not basis SharePoint Search.

    ReplyDelete
    Replies
    1. Not search at all. It's Microsoft Graph, not Office Graph, and the endpoint has been documented by Microsoft after I wrote this post.

      And the post clearly talked about SPO, but good point anyways :)

      Delete