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, 4:28 AM
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, 5:41 AM
Thank you It is Working Fine
Prajakta PatilPosted May 15, 2018, 4:11 AM
I am getting 0 values with the mentioned query. Is there any pre-requisite for above code to work?
shipra mamgainPosted Apr 18, 2018, 7:37 AM
It is giving null value for ViewsLifeTime
Jyothsna SallaPosted May 5, 2017, 10:46 AM
Can we get monthly view count for top 5 pages in site coll?
Gowtham RajamanickamPosted Apr 2, 2016, 5:22 AM
Good one...
Humayun Kabir MamunPosted Mar 27, 2016, 7:50 AM
Nice...
Abhay ShankerPosted Mar 26, 2016, 7:43 AM
Nice Share
Debasis SahaPosted Mar 26, 2016, 12:42 AM
Nice one..
Kumaresh RajalingamPosted Mar 25, 2016, 10:58 AM
Nice one
Sibeesh VenuPosted Mar 25, 2016, 5:41 AM
Nice Share
Debasis SahaPosted Mar 25, 2016, 12:17 AM
Good
Saillesh PawarPosted Mar 25, 2016, 12:06 AM
Nice share sir
Vignesh ManiPosted Mar 24, 2016, 5:44 PM
nice