Wednesday, March 27, 2019

Getting tasks and sub-tasks from task list using SharePoint 2013/SP online and rest API


var fullurl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Project%20Tasks')/items?$select=*,ParentID/Id,Predecessors,Predecessors/Id&$expand=ParentID,Predecessors";
    $.ajax 
({ 
url: fullurl, 
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var items = data.d.results.length;
if(items > 0) 

var tasks = data.d.results; 

jsonObj = [];
var mainTasks = [], subTasks = [];
$.each(tasks, function (index, task) {
if(task.ParentID.Id) {
item = {}
item ["ParentID"] = task.ParentID.Id
item ["Title"] = task.Title
item ["Priority"] = task.Priority
item ["DueDate"] = task.DueDate
item ["StartDate"] = task.StartDate
item ["PercentComplete"] = task.PercentComplete
item ["Status"] = task.Status
subTasks.push(item);
} else {
item = {}
var startdate = $.datepicker.formatDate( "dd-mm-yy", new Date(task.StartDate));
var duedate = $.datepicker.formatDate( "dd-mm-yy", new Date(task.DueDate));
item ["ID"] = task.Id
item ["Title"] = task.Title
item ["Priority"] = task.Priority
item ["DueDate"] = duedate
item ["StartDate"] = startdate
item ["PercentComplete"] = task.PercentComplete
item ["Status"] = task.Status
mainTasks.push(item);
}
});
},
error: function (error) {
alert(JSON.stringify(error));
}
});  

2 comments:

  1. Can you give an example of how and when to use this?

    ReplyDelete
  2. in your code you end up with two different lists (mainTasks, and subTasks) with no apparent relationship. So, for any task in mainTask, how do you know which sub task in subTasks belong to it?

    ParentID.Id only exists in the subTasks. So, if there is a subTask with ParentId of 32 there is no way to find the Parent Id that corresponds with 32.

    ReplyDelete

SharePoint - Cannot convert a primitive value to the expected type 'Edm.Double'. See the inner exception for more details If y...

Ad