Home › Forums › Classic DFFS › API call in edit to show all open items by owner
- This topic has 6 replies, 2 voices, and was last updated 4 years, 9 months ago by becca.
-
AuthorPosts
-
-
February 12, 2020 at 18:46 #28643
I’m trying to add a chunk of text to the edit page for helping triage tasks between team members by showing a count of open items by team member, but in just testing to get a count of items I keep getting errors. I’ve tested the $select and $filters in another tab and they work, but I can’t get it to work on the page. Can you show me where I’m going wrong or suggest a different way?
A few different ways I’ve tried;
function CountListItems(){ $("#ListData").html(""); var sum = 0; $.ajax({ url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('demo')/items/?$select=Title&$filter=Status eq 'In Triage'", type: "GET", headers: { "Accept":"application/json;odata=verbose", }, success: function (data) { var sum = data.d.results.length; alert(sum); } error: function (data) { alert("error"); } }); } SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CountListItems);
Always gives “error” alert
function onSuccess(sender, args) { var count = this.list.ItemCount; alert(count); } function onFail(sender, args) { alert("fail"); } var ctx = SP.ClientContext.get_current(); this.list = ctx.get_web().get_lists().getByTitle("demo"); ctx.load(list); ctx.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFail));
Always gives “undefined” for count
var siteurl = _spPageContextInfo.webAbsoluteUrl; $.ajax({ url: siteurl + "/_api/web/lists/getbytitle('demo')/items", method: "GET", headers: { "Accept": "application/json; odata=verbose"}, success: function (data) { if (data.d.results.length > 0) { var sum = 0; var sum1 = 0; $.each(data.d.results, function(item){ if(item.Status === "New/In Review") sum++; if(item.Status === "In Triage") sum1++; }); alert(sum); alert(sum1); } }, error: function (data) { alert("Error: " + data); } });
Always gives “0” for both sums (should be “16” and “1”)
-
February 12, 2020 at 19:54 #28647
I’m not sure what is wrong without looking at the actual error you receive (by console.log the data in the error function. I did clean it up a bit and set up an example for you here:
function countItemsByStatus(arg) { var deferred = jQuery.Deferred(); jQuery.ajax({ "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + arg.listDisplayName + "')/items?$filter=" + arg.filterField + " eq '" + arg.value + "'", "type": "GET", "headers": { "Accept": "application/json;odata=verbose", }, "success": function (data) { // Use console.log to see the data object // console.log(data); deferred.resolve(data); }, "error": function (err) { deferred.reject(err); } }); return deferred.promise(); } countItemsByStatus({ "listDisplayName": "demo", "filterField": "Status", "value": "In Triage" }).done(function (data) { var count = data.d.results.length; alert(count); }).fail(function (err) { alert(JSON.stringify(err)); });
Alexander
-
February 12, 2020 at 22:45 #28650
This is perfect, I added additional filters that will always be true to the initial url. Now I just need to put the variable out and use it to feed a html section. This is great and helps our form a ton, thank you!
-
February 13, 2020 at 18:26 #28663
Just got a different scenario, is there a way to get a sum of all the values in a field across multiple items? So instead of count of everything filtered, sum of all values of a certain field for everything filtered. Was originally going to be all open items per team member but now it may change to total story points per team member and I’m not sure how to change the inside of countItemByStatus for that.
-
February 13, 2020 at 20:39 #28681
Like this:
countItemsByStatus({ "listDisplayName": "demo", "filterField": "Status", "value": "In Triage" }).done(function (data) { var count = 0; jQuery.each(data.d.results, function (i, item){ if(item.YOUR_FIELD !== null){ count += parseInt(item.YOUR_FIELD); } }); alert(count); }).fail(function (err) { alert(JSON.stringify(err)); });
Alexander
-
February 13, 2020 at 21:20 #28684
Thanks a ton, this thread has given me really good js notes.
In case anyone in the future is searching for something similar here’s my code for displaying a count of open items per team member directly on the edit screen with an indicator for > 5 open items;function countItemsByStatus(arg) { var deferred = jQuery.Deferred(); jQuery.ajax({ "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + arg.listDisplayName + "')/items?$filter=Status eq 'In Progress' and Team eq 'B-Team' and " + arg.filterField + " eq '" + arg.value + "'", "type": "GET", "headers": { "Accept": "application/json;odata=verbose", }, "success": function (data) { // Use console.log to see the data object // console.log(data); deferred.resolve(data); }, "error": function (err) { deferred.reject(err); } }); return deferred.promise(); } countItemsByStatus({ "listDisplayName": "demo", "filterField": "OwnerId", "value": "10" }).done(function (data) { document.getElementById("tom").innerHTML = data.d.results.length; if (data.d.results.length > 5) { document.getElementById("tom").style = "color:red"; } }).fail(function (err) { alert(JSON.stringify(err)); });
Where “tom” refers to a <span> in a table in an HTML section in the tab. The countItemByStatus is repeated for each team member, but I’m sure someone could leverage the previous comments to cycle through all instead.
-
February 19, 2020 at 18:34 #28745
-
-
AuthorPosts
- You must be logged in to reply to this topic.