API call in edit to show all open items by owner

Forums Dynamic Forms for SharePoint API call in edit to show all open items by owner

Viewing 6 reply threads
  • Author
    Posts
    • #28643
      becca
      Participant

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

    • #28647
      Alexander Bautz
      Keymaster

      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

    • #28650
      becca
      Participant

      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!

    • #28663
      becca
      Participant

      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.

    • #28681
      Alexander Bautz
      Keymaster

      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

    • #28684
      becca
      Participant

      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.

    • #28745
      becca
      Participant

      The jQuery.each isn’t working, I have that section copied exactly with an alert after the first { and it’s never reached

      Sorry, wasn’t paying enough attention to filters

      • This reply was modified 4 days, 11 hours ago by becca.
Viewing 6 reply threads
  • You must be logged in to reply to this topic.