Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.5k views
in SharePoint Server by
edited by anonymous

In SharePoint Server, I have an OOTB Approval workflow, and I am trying to implement JQuery script in Script Editor Web Part to show the total number of approved and rejected workflow requests in SharePoint List as below

SharePoint Approval Workflow Status

But it seems that I have something wrong with my JS code where the number of approved and rejected items are not calculated as expected, Could you please help me to know How to get items count per SharePoint Workflow Approval Status using JavaScript where the workflow is OOTB Approval workflow.


<body oncontextmenu="return false" onkeydown="return false;" onmousedown="return false;">
<div class="container">
		<div class="box requestBox">
		<span class="count request"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Requests</span>
		</div>
		<div class="box empBox">
		<span class="count emp"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Employee</span>
		</div>
		<div class="box approvedBox">
		<span class="count approved"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Approved</span>
		</div>
		<div class="box rejectedBox">
		<span class="count rejected"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Rejected</span>
		</div>
		<div class="box inprogressBox">
		<span class="count inprogress"><img src="	ajax-loader.gif"/></span><br>
		<span class="text">In Progress</span>
		</div>
		<div class="box validBox">
		<span class="count valid"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Valid</span>
		</div>
		<div class="box expBox">
		<span class="count exp"><img src="ajax-loader.gif"/></span><br>
		<span class="text">Expired</span>
		</div>
	</div>
</body>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

	getMyListUrl = "/_api/Web/Lists/GetByTitle('test')/items";   //change the "myList" with your list's internal name

	getItems(getMyListUrl).done(function(data){

		var requestCount = data.d.results.length;
		var empArr = [];
		var approvedCount = 0;
		var rejectedCount = 0;
		var inprogressCount = 0;
		var validCount = 0;
		var expireCount = 0;


		data.d.results.forEach(function(item){

			empArr.push(item.EmployeeName);   // change the EmployeeName with your column's internal name

			if(item.ODATA__ModerationStatus == "Approved") approvedCount += 1 ; // change the Approval_x0020_Status with your column's name
			if(item.ODATA__ModerationStatus == "Rejected") rejectedCount += 1 ;
			if(item.ODATA__ModerationStatus == "In Progress") inprogressCount += 1 ;
			if(item.Validity_x0020_Status == "Valid") validCount += 1;   // change the Validity_x0020_Status with your column's internal name

			var d = new Date(item.Expiration_x0020_date);    // change the Expiration_x0020_date with your column's internal name
			var currentDate = new Date();

			if(d > currentDate) expireCount += 1;

		});

		var empArrOutput = empArr.filter(function(v, i, self){

			return i == self.indexOf(v);

		});

		$(".request").text(requestCount);
		$(".emp").text(empArrOutput.length);
		$(".approved").text(approvedCount);
		$(".rejected").text(rejectedCount);
		$(".inprogress").text(inprogressCount);
		$(".valid").text(validCount);
		$(".exp").text(expireCount);


	});

	document.onkeydown = function(e) {
	if(event.keyCode == 123) {
	return false;
	}
	if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)){
	return false;
	}
	if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)){
	return false;
	}
	if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
	return false;
	}
	}

});


function getItems(url) {

	    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
		async: true,
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {

        },
        error: function (error) {
            console.log(JSON.stringify(error));
        }
    });

}
</script>
</html>

 


1 Answer

2 like 0 dislike
by 159 200 385
selected by anonymous
 
Best answer

Looks like the issue is mainly related to the Internal column name of the Approval Status column as well as the assigned Workflow Approval Status value at this line in your code:

if(item.ODATA__ModerationStatus == "Approved")

So in your case, you should do the following:

  1. replace this column name ODATA__ModerationStatus with the internal column status name.
  2. Assign the Workflow Approval Status Value to the corresponding SharePoint Workflow Approval Status Code as below:

SharePoint Workflow Status Code

SharePoint Workflow Status Value                     SharePoint Workflow Status Code
---------------------------------------------------------------------------------------------------------------------
Not Started                                                                                   0
Failed on Start                                                                               1
In Progress                                                                                   2
Error Occurred                                                                               3
Canceled                                                                                      4
Completed                                                                                    5
Failed on Start (retrying)                                                                  6
Error Occurred (retrying)                                                                  7
Canceled                                                                                      15
Approved                                                                                     16
Rejected                                                                                      17

Based on the above info, you have to update your code with the correct workflow internal column name and the corresponding SharePoint workflow approval status code as below

if(item.Approve == "16") approvedCount += 1 ; 
 if(item.Approve == "17") rejectedCount += 1 ;
 if(item.Approve == "2") inprogressCount += 1 ;

As per my test on my side, I got the below output

How to get items count per SharePoint Workflow Approval Status using JavaScript

If you don’t ask, the answer is always NO!
...