Ram Prasad
 

SharePoint 2013: Showing Page Views within a SharePoint Page

Oct, 14 2014
 
3 min/s
 
 

Description

Showing the Page Views within a SharePoint page is a commonly expected feature by many of my clients. It looks good and useful to show the number of times a news/event page is viewed in one’s corporate intranet. In SharePoint 2010, this was achieved by reading the Request Usage tables from the WSS Logging database and the consolidated count for each page/item is processed and stored within an application (mostly a custom SQL table). But the analytics in SharePoint 2013 makes this easy as they store the consolidated view count within the Search Service Application. This article describes how to show the 'Views Count' for a page using JavaScript client object model.

The Blueprint

So, how are we going to design this?
Let’s make use of the SharePoint 2013 Search Infrastructure to achieve this. First, we have to fire a query on the Search Service so that only the current page (or the page in question) is returned in the result. Now, from the properties of this result which is returned by search service, there will be a property named ‘ViewsLifeTime’, which gives the life time view count of a page. For querying, use either the page ‘url’ or the ‘guid’ to get the search result of the current page.

The Building Blocks

In this article I will be using the GUID of the page for querying the search index. Note: We can also use the URL instead of GUID, but I prefer GUID for the following reasons

  • If we change the URL, our search query may not return results until the next crawl is done.
  • If we have Minimal Download Strategy feature enabled for a site, the page URL (which is shown in the browser address bar) may not be same as the URL stored in the search index. We will end up writing JSOM code to read the current page’s URL.

Create a Managed Property

We need a managed property to query based on the page's GUID. Within the Search Schema of your SharePoint Farm, create a managed property with the following details.

Property Name PageGuid
Type Text
Queryable Yes (Checked)
Complete Matching Yes (Checked)
Mappings to crawled properties Add mapping to 'ows_UniqueID'

After creating this Managed Property, start a full crawl of the appropriate content source/s. For SharePoint Online, set the appropriate Site Collections to be re-indexed

Get the GUID of the current page

There is a JavaScript variable called – '_spPageContextInfo' available within a SharePoint page using which we can get the GUID of the current page. Below is a JS CSOM snippet to fetch the GUID

var context;
    var web;
    var list;
    var currentItem;

$(document).ready(function () { context = new SP.ClientContext.get_current(); web = parentContext.get_web(); list = web.get_lists().getById(_spPageContextInfo.pageListId); currentItem = list.getItemById(_spPageContextInfo.pageItemId) context.load(currentItem); context.executeQueryAsync(onQuerySucceeded, onQueryFailed); });

function onQuerySucceeded() { var guid = currentItem.get_fieldValues("UniqueId").UniqueId.toString(); }

function onQueryFailed(sender, args) { //Error Logging }

Query for the Page

Using the JS CSOM, fire a query with this GUID to get the page details.

function onQuerySucceeded() {
    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
    keywordQuery.set_queryText('PageGuid:"{' + currentItem.get_fieldValues("UniqueId").UniqueId.toString() + '}"');
    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
    results = searchExecutor.executeQuery(keywordQuery);
    context.executeQueryAsync(SearchDone, SearchFailed)
}

function SearchDone() { var viewCount = results.m_value.ResultTables[0].ResultRows[0].ViewsLifeTime; //Use JQuery to show the viewCount on the page }

function SearchFailed(sender, args) { //Error Logging }

The Build

All the blocks explained above can be merged as a working code and can be added to a page through a Script Editor Web Part, or can be added directly into a page layout which reflects in all the pages created using this layout. Also, reference to the following files needs to be added

  • init.js
  • sp.runtime.js
  • sp.js
  • sp.search.js

Conclusion

By using the JS COM, we can assemble the blocks defined above and show the page views for a page. Thank you SharePoint 2013 for making it so easy :)