Update for Mavention Last Modified Documents available

Update for Mavention Last Modified Documents available header image

Not that long ago we released the first version of the Mavention Last Modified Documents. The Mavention Last Modified Documents will retrieve all the sites the user is following and use them to construct an overview of last modified documents within these sites.

After implementing this app by several customers we found out that for some users it stopped showing their latest documents but would turn up with an error. As it turned out these issues appeared to be related to the amount of sites a user would follow. After a certain amount of sites (usually 15) being followed the App would break and no longer show the desired results.

When working with search queries there are two options. You can either use the GET request to query your results, or use a POST to the same REST service. Getting results through a simple REST call is pretty straight forward, and in the first version of the app we used that approach. You can configure everything in the URL you pass and will be provided with the results

var searchCall = Mavention.LastModifiedDocuments.appWebUrl + "/_api/search/query?querytext='" + actualQuery + "'&sortlist='" + Mavention.LastModifiedDocuments.sortingManagedProperty + ":" + Mavention.LastModifiedDocuments.sortingDirection + "'&rowlimit=" + Mavention.LastModifiedDocuments.numberOfDocumentsToRetrieve + "&selectproperties='Title,Path,SiteTitle,ModifiedBy,ModifiedOWSDATE,"+ Mavention.LastModifiedDocuments.sortingManagedProperty +"'";

$.ajax({
    url: searchCall,
    headers: {
        "accept": "application/json;odata=verbose"
    },
    success: Mavention.LastModifiedDocuments.modifiedContentRetrieved,
    error: Mavention.LastModifiedDocuments.requestFailed
});

Unfortunately as you can imagine if the actualQuery gets to long there is a risk that the URL ends up to many characters for the browser to handle. As in our case where we use the Site URI to determine our results and we do not limit the amount of sites to follow a best practice would be to use POST requests rather then GETS as they URL could potentially become huge. You can find a full description of the workings on MSDN – SharePoint Search REST API overview.

In order to achieve the same results as the get query you will have to create a JSON object that you can pass with the post, something that takes a little bit more work then to write the query directly. If the you have the JSON object the only thing that then stands in your way is the RequestDigest, something that is required to do POST requests. You can find a nice write-up on how to use (and refresh) in the article How to refresh the Request Digest value in JavaScript by Wictor Wilén. If you pas an invalid request digest value you will get a 403 error that you are not allowed to do that request. If you pass a valid value you will get the same results as before.

var postData = JSON.stringify(
{
    'request':
       {
           '__metadata': { 'type': 'Microsoft.Office.Server.Search.REST.SearchRequest' },
           'Querytext': actualQuery,
           'SelectProperties': {
               'results': ['Title', 'Path', 'SiteTitle', 'ModifiedBy', 'ModifiedOWSDATE', Mavention.LastModifiedDocuments.sortingManagedProperty]
           },
           'RowLimit': Mavention.LastModifiedDocuments.numberOfDocumentsToRetrieve,
           'SortList': {
               'results': [
                   {
                       'Property': Mavention.LastModifiedDocuments.sortingManagedProperty,
                       'Direction': sortDirection
                   }
               ]
           }
       }
});

$.ajax({
    url: Mavention.LastModifiedDocuments.appWebUrl + "/_api/search/postquery",
    type: "POST",
    data: postData,
    headers: {
        "accept": "application/json; odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": Mavention.LastModifiedDocuments.requestDigest
    },
    success: Mavention.LastModifiedDocuments.modifiedContentRetrieved,
    error: Mavention.LastModifiedDocuments.requestFailed
});

As you can imagine using the POST request rather then the GET requests allows for some more flexible solutions but will also require a bit more JavaScript to get it working.

If you already have a version of the Mavention Latest Documents installed, all you need to do is update the app. You can find a short description on updating an app on MSDN How to: Update apps for SharePoint. If you install a new version you still need to configure it as described in the app or in this previous blog: Mavention Latest Documents available in the Office Store.

You can find the Mavention Latest Documents in the Office Store, or checkout all Apps by Mavention.

Loading comments…