SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've enabled Auditing in a list that keeps track of how many times an item has been viewed.

I would now like to use a Content Query Web part to return a list of documents based on their popularity (view/download count)

I tried searching for a solution but it looks like there's nothing out of the box that can do this. Any ideas what I should do to accomplish this?

Thanks a lot for any assistance!

Mr. J

share|improve this question
up vote 1 down vote accepted

I got this working in the end using JavaScript. The results are not 100% accurate because of the way SharePoint caches item values but it's accurate enough to give a rough indication how many times an item was downloaded.

Here's the steps I went through:

  1. I first created a column called Popularity in the document library. Type number.

  2. Then I created a new XSL template in ItemStyle.xsl that adds the Popularity column and the ID of the item to the returned value (hidden from user with css). This template is a version of the one called WithDocIcon because I want to display the icon next to the title as well.

    <xsl:template name="WithDocIconAndPopularity" match="Row[@Style='WithDocIconAndPopularity']" mode="itemstyle">
       <xsl:variable name="SafeLinkUrl">
            <xsl:call-template name="OuterTemplate.GetSafeLink">
                 <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
       <xsl:variable name="DisplayTitle">
            <xsl:call-template name="OuterTemplate.GetTitle">
                <xsl:with-param name="Title" select="''"/>
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
                <xsl:with-param name="UseFileName" select="1"/>
       <div class="item link-item">
           <xsl:if test="string-length(@DocumentIconImageUrl) != 0">
               <div class="image-area-left">
                   <img class="image" src="{@DocumentIconImageUrl}" title="" />
           <div class="link-item cqwp-popularity-item">
               <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
               <a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
                   <xsl:if test="$ItemsHaveStreams = 'True'">
                     <xsl:attribute name="onclick">
                       <xsl:value-of select="@OnClickForWebRendering"/>
                   <xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'">
                     <xsl:attribute name="onclick">
                       <xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
                   <xsl:value-of select="$DisplayTitle"/>
               <div class="description">
                   <xsl:value-of select="@Description" />
               <div class="cqwp-item-id">
                   <xsl:value-of select="@ID" />
               <div class="cqwp-item-popularity">
                   <xsl:value-of select="@Popularity" />
  3. Inside a JavaScript file's $(document).ready(function() {}) I added a listener to the links inside a div with class cqwp-popularity-item:

    $('.cqwp-popularity-item a').click(downloadCounter);
  4. Then, my downloadCounter function looks pretty much like this:

    var href='';
    function downloadCounter(e)
        // increment counter
        var t = $(this);
        var iId =;
            var id = iId.html(),
                popularity =,
                listTitle = '';
            href = t.attr('href');
                    // add to this if-else all the lists that you know have a Popularity column
            if(href.indexOf('LIST TITLE') != -1)
                listTitle = 'LIST TITLE';
            if(listTitle != '')
                var siteUrl = _spPageContextInfo.siteServerRelativeUrl; // add subsite to this if required
                var clientContext = new SP.ClientContext(siteUrl);
                var oList = clientContext.get_web().get_lists().getByTitle(listTitle);
                oListItem = oList.getItemById(id);
                oListItem.set_item('Popularity', parseInt(popularity)+1);
                function onQuerySucceeded() {
                                    // open the document link in a new window
                function onQueryFailed(sender, args) {
                    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
                return false;
        return true;

I know the code is not the most efficient & streamlined but this fits my requirements quite well.

Hopefully this will help someone else as well!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.