Introduction
The popular documents can be viewed manually from the document libraries withthe popular items tab. In this article, you will learn how to fetch popular documents based on view counts using JSOM and REST API. This will be applicable for all SharePoint platforms.
Prerequisite
You have to enable the reporting feature under site collection features in the site to see the popular documents. This data can be retrieved using search queries. Basically this is part of SharePoint analytics.
Using JSOM
- Set and get the necessary context. Using the context, initiate the keyword query function.
- var searchContext = new SP.ClientContext();
- var searchQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(searchContext);
- The results can be retrieved using multiple query filters. In this case, I am using path variable to filter the data. This helps us in getting results for one particular library. Multiple document library data can also be viewed by setting the filters with 'and' parameter. Set the query text as below.
- searchQuery.set_queryText("path:http://siteurl/Documents”)
- The following syntax helps us in executing the query against the search server by initializing a SearchExecutor instance.
- var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(searchContext);
- Initialize the results and execute the request.
- searchRes = searchExecutor.executeQuery(searchQuery);
- searchContext.executeQueryAsync(SearchResults, QueryFailed);
- In the success function, expand the result set (searchRes) to see the document results. The following code snippet shows you to parse the search results.
- var resultSet = searchRes.m_value.ResultTables[0].ResultRows;
- var popularDocs = new Array();
- for(var i = 0; i < resultSet.length; i++)
- {
- popularDocs[i] = [resultSet[i].Title,resultSet[i].ViewsLifeTime];
- }
- Then sort the result set on descending order based on the view count using the below logic.
- function Sort(a, b) {
- return (a[1] > b[1] ? -1 : (a[1] < b[1] ? 1 : 0));
- }
- Now popularDocs array will have the result set sorted based on the view count.
- popularDocs.sort(Sort);
The Search REST API can also be used to fetch the view counts for the documents. Again we will use path parameter to filter the results for one particular document library.
We will build the rest query first. In the query URL, you can set the necessary filters required. The advantage of using REST is sorting can be done by setting sortlist parameter. Please find the below query URL.
This will fetch all the document related items and we will filter out the necessary values using the code logic. JQuery Ajax call is used to fetch the results.
The below code snippet shows the implementation.
- $.ajax({
- url: "/_api/search/query?querytext='path:http://siteurl/Documents'&sortlist='ViewsLifeTime:descending'",
- method: "GET",
- headers: {"accept": "application/json;odata=verbose",
- },
- success: function(data){
- var restResults = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
- var popularRestResults = new Array();
- for(var i=0;i<restResults.length;i++){
- popularRestResults[i] = [restResults[i].Cells.results[3]["Value"],restResults[i].Cells.results[20]["Value"]];
- }
- },
- error: function(sender,args){
- }
- });


Chakravarthy KukkalaPosted Jun 12, 2019, 12:28 AM EDT
Newly logged in users are not able to see the views count . Please share any Workaround solution for this issue. May be next day they are able to see the content.
Chakravarthy KukkalaPosted Mar 26, 2019, 1:41 AM EDT
Thank you It is Working Fine
Prajakta PatilPosted May 15, 2018, 12:11 AM EDT
I am getting 0 values with the mentioned query. Is there any pre-requisite for above code to work?
shipra mamgainPosted Apr 18, 2018, 3:37 AM EDT
It is giving null value for ViewsLifeTime
Jyothsna SallaPosted May 5, 2017, 6:46 AM EDT
Can we get monthly view count for top 5 pages in site coll?
Gowtham RajamanickamTOP 100Posted Apr 2, 2016, 1:22 AM EDT
Good one...
Humayun Kabir MamunPosted Mar 27, 2016, 3:50 AM EDT
Nice...
Abhay ShankerPosted Mar 26, 2016, 3:43 AM EDT
Nice Share
Debasis SahaTOP 100Posted Mar 25, 2016, 8:42 PM EDT
Nice one..
Kumaresh RajalingamPosted Mar 25, 2016, 6:58 AM EDT
Nice one
Sibeesh VenuTOP 100Posted Mar 25, 2016, 1:41 AM EDT
Nice Share
Debasis SahaTOP 100Posted Mar 24, 2016, 8:17 PM EDT
Good
Saillesh PawarPosted Mar 24, 2016, 8:06 PM EDT
Nice share sir
Vignesh ManiTOP 100Posted Mar 24, 2016, 1:44 PM EDT
nice