How to get the count of Title in sharepoint using Dataform web part or Javascript

Hi,

I have a SharePoint List which is used to store the information of course enrollment.

What i am looking for is to get the count and name of Course for which maximum people have enrolled?


  • Edited by NirajZ Monday, August 17, 2015 1:30 PM
August 17th, 2015 1:17pm

Best option would be to create a view and group by title field. This will show you the count.
Free Windows Admin Tool Kit Click here and download it now
August 17th, 2015 5:57pm

Hi Prasath,

I know creating a view will give me count, what i am looking for is:

to get the count and name of Course for which maximum people have enrolled? and i will use this information on front end for further process.

Please elaborate your approach
  • Edited by NirajZ Monday, August 17, 2015 6:04 PM
August 17th, 2015 6:04pm

Hi,

We can use REST API to get all the list item, then use JavaScript to group by the data. The following code for your reference:

<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
 var siteURL = _spPageContextInfo.webAbsoluteUrl;
 var listname = "CustomList";
 var url = siteURL + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title";
 $.ajax({
	 url: url,
	 method: "GET",
	 headers: { "Accept": "application/json; odata=verbose" },
	 success: function (data) {
		var arr=new Array();
		var items = data.d.results;
		for(var i = 0; i < items.length;i++) {			 
			arr.push(items[i].Title);
		}
		
		// group by the data 
		var hist = {};
		arr.map(function (a) { if (a in hist) hist[a] ++; else hist[a] = 1; } );
		console.log(hist);
	 },
	 error: function (data) {		
	 }
 });
});
</script>

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 9:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics