blog.scoreman.net

Aggregating tasks using SharePoint Search REST API

This is a short example of how you can use SharePoint Search REST Search to aggregate all tasks assigned to the current user that are not set to completed. The example is a SharePoint-hosted app that makes two REST calls. The first one is for retrieving the full name of the user currently logged in. The second call is to the search API and executes the query. For this app to Work you will have to add QueryAsUserIgnoreAppPrincipal permisson.

For the filtering of all tasks, except the ones with status set to completed, to Work the crawled properties ows_Status will have to be added to the managed property Status. If you want to list all tasks regardless of status just remove “NOT Status:Completed”.

'use strict';

jQuery(document).ready(function () {
    getCurrentUserDisplayName().then(getUserTasks).then(displayResult);
});

function getCurrentUserDisplayName() {
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser";
    return jQuery.ajax(url, { method: "GET", headers: { "accept": "application/json;odata=verbose" } });
}

function getUserTasks(data) {
    var currentUserDisplayName = data.d.Title;
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?selectproperties='Title,Path'&querytext='ContentTypeId:0x0108* NOT Status:Completed AND AssignedTo:" + currentUserDisplayName + "'";
    return jQuery.ajax(url, { method: "GET", headers: { "accept": "application/json;odata=verbose" } });
}

function displayResult(data) {
    var viewModel = function () {
        var self = this;
        self.tasks = ko.observableArray();

        jQuery.each(data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results, function () {
            self.tasks.push({
                title: ko.observable(getValueByKey('Title', this.Cells.results)),
                url: ko.observable(getValueByKey('Path', this.Cells.results))
            });
        });
    };

    ko.applyBindings(new viewModel());
}

function getValueByKey(key, results) {
    var postItem = jQuery.grep(results, function (e) {
        if (e.Key === key)
            return e;
    })[0].Value;

    return postItem;
}

In displayResult I’m using Knockout JavaScript library to bind the result to my UI but you are free to do whatever you you want with the result. Thanks to Cas van Lersel for the helper function getValueByKey that parses the key value pairs that are returned by SharePoint.

Tags: ,

3 Responses to “Aggregating tasks using SharePoint Search REST API”

  1. Mihir Says:

    This is awesome.. thanks i am looking for exactly same thing… 🙂

  2. Praveen Says:

    With this approach we can only able to get the managed properties as it is using search service right? But, I need to get the Due Date, Completed, Percentage Complete also. Is there any thing we can get these 3 fields as well?

    thanks much.

  3. Johan Says:

    @Praveen, yes that is correct. You will have to create managed properties for the crawled properties: ows_DueDate, ows_Checkmark, ows_PercentComplete. This can be done both in Office 365 and on-premise. If your are using O365 it could take quite some time before the managed properties return data.

Leave a Reply