Posts Tagged ‘SharePoint 2013’

Aggregating tasks using SharePoint Search REST API

Tuesday, August 20th, 2013

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”.

[code lang=”JavaScript”]
‘use strict’;

jQuery(document).ready(function () {

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 () {
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;

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.">Entries (RSS) and Comments (RSS).