Display total no.of Projects in the Project Center based on the user Permission


Hello Readers!

In this blog I’m gonna discuss about displaying the total number of projects count on the PWA project center page based on the user permission using JSOM.

Below is the custom code to read and display the project count. Add this code to a CEWP as I discussed in my previous blogs.

<script type="text/javascript" src="/PWA/SiteAssets/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.debug.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.debug.js"></script>
<script type="text/javascript" src="/_layouts/15/ps.debug.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.UI.dialog.js"></script>

<div><h2 id="spanMessage1"></h2></div>

<script type="text/javascript">
var projContext;
var projects;
var TotProjects;

SP.SOD.executeOrDelayUntilScriptLoaded(ReadProjects, "PS.js");

function ReadProjects(){
projContext = PS.ProjectContext.get_current();
projects = projContext.get_projects();
projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

function onQuerySucceeded(response) {
var enumerator = projects.getEnumerator();
$('#spanMessage1').text('Total projects : ' + TotProjects);

function onQueryFailed(sender, args) {
$('#spanMessage1').text('Request failed: ' + args.get_message());

below is the output screenshot, this count will dynamically change based on the user permissions.