Wednesday 4 November 2015

_api/me/getrecentdocs stopped working! How can I replace it?

Hi there,

So, as it seems, one of our favourite endpoints stopped working. This morning I had a call from one of our clients saying they were unable to view their recent documents in the homepage.

I did a quick investigation and I faced this strange error when trying to call my-site.sharepoint.com/_api/me/getrecentdocs:












So I started to read about this and it seems that this API stopped working a few days ago (guess nobody noticed before!?). Waiting for a (possible) fix was not an option, so I started to develop a workaround for this.

Here's the code of our previous solution:

        var uri = String.format("{0}/_api/me/getrecentdocs/?$top={1}&$filter=startswith(LinkLocation, '{2}')&$select=FileName,LinkLocation,Application", serverUrl, rowLimit, encodeURIComponent(belowWebUrl));

        // Query
        jQuery.ajax({
            url: uri,
            dataType: "json",
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=nometadata" }
        })

When the request was completed (.done), we were getting the array of object like this:

        // Render results
        var arrayOfDocuments = result.value;

For each object within the array (d), it was simple to get property values, using "d.PropertyName". Ex: d.FileName

This solution was really simple, but since it isn't working any more, let's jump into the workaround!

Solution (using the search api):

Let's build our request url, passing the base Uri, query text, query template, selected properties, row limit and sort it descending, using the last modified date (getting the most recent on top, to achieve the same result).

        var baseUri = serverUrl + "/_api/search/query";
        var queryText = "querytext='*'";
        var queryTemplate = "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)'";
        var selectProps = "selectproperties='Path,Filename,SPWebUrl'";
        var uri = baseUri + "?" + queryText + "&" + queryTemplate + "&rowlimit=" + rowLimit.toString() + "&bypassresulttypes=false&" + selectProps + "&sortlist='LastModifiedTime:descending'&enablesorting=true"; 
        
        // Query
        jQuery.ajax({
            url: uri,
            dataType: "json",
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=nometadata" }
        })

Now, the way we are going to get our array will be a bit different:

        var arrayOfDocuments = result.PrimaryQueryResult.RelevantResults.Table.Rows; 

In order to have the same easiness on accessing the object's properties (d.FileName), we will have to simplify the data structure, like this:

        // Simplify the data strucutre
        var arrayOfDocumentsSimplified = [];
        _.each(arrayOfDocuments, function (d) {
            var doc = {};
            _.each(d.Cells, function (c) {
                doc[c.Key] = c.Value;
            });
            arrayOfDocumentsSimplified.push(doc);
        });

And that's it! Our most recent documents are showing up again :)





















Hope you find it useful!

RS

1 comment: