+ Start a Discussion

How do I display a Knowledge Article's "Most Viewed" on a custom VisualForce page?

I have a VisualForce Component that looks like this:


    <apex:stylesheet value="{!URLFOR($Resource.custom_table_template)}"/>
        <table id="articles">
        <th id="number">Article Number</th>
        <th id="title">Article Name</th>
        <th id="label">Article Type</th>
        <th id="date">Last Published Date</th>
        <knowledge:articleList articleVar="article" sortBy="mostViewed" pageSize="5">
            <td><a target="_parent" href="{!URLFOR($Action.KnowledgeArticle.View, article.id)}">{!article.title}</a></td>

How would I add a column that displays the "Most Viewed" of that article?


"Most viewed" is not an available column. If you want to get this information, you will have to run a SOQL query on KnowledgeArticle__ViewStat.


How would I do that in the VisualForce component?


So I did some googling, and I have some code now.


This is my Apex class:


global with sharing class Component_Query {
global String queryString {get; set;}
global List<sObject> results {get{
List<sObject> result = Database.Query(queryString);
return result;
} set; }


This is my Visualforce Component:


<apex:component controller="Component_Query" access="global">
  <apex:attribute name="QueryString" type="String" required="true" access="global" assignTo="{!queryString}"
    description="A valid SOQL query in string form." />
    <apex:variable value="{!results}" var="results"/>
    <apex:componentBody />


and this is my Visualforce Page


<apex:page >
    <knowledge:articleList articleVar="article" sortBy="mostViewed" pageSize="5">
        <c:Query_component queryString="SELECT NormalizedScore FROM KnowledgeArticleViewStat WHERE Id = '{!article.id}'">
            <apex:repeat value="{!results}" var="result">


I put this page in a tab, but when I open the tab, nothing shows up.


Did you check if "article.id" is Knowledge Article Version Id or Knowledge Article Id? It can explain why the result is empty


KnowledgeArticleViewStat uses ParentId to reference KnowledgeArticle, so I changed:


<c:Query_component queryString="SELECT NormalizedScore FROM KnowledgeArticleViewStat WHERE Id = '{!article.id}'">




<c:Query_component queryString="SELECT NormalizedScore FROM KnowledgeArticleViewStat WHERE ParentId = '{!article.id}'">


Based on this:


It seems "article.id" is  the Knowledge Article Id.


Is my apex code for retrieving the value correct?

      Have you solved this problem?
     I am facing the same problem. I want to show the viewstat with every article by using this <knowledgeArticleList> component.
   I am not getting how to do this so if you have done this then please help me 
Rishav kumar
I'm not developing in VisualForce, but figured out from the API to make sure and use the KnowledgeArticleVersion.KnowledgeArticleId in your SOQL. I was using the KnowledgeArticleVersion.Id when querying KnowledgeArticleViewStat object. In short what worked for me was: SELECT NormalizedScore from KnowledgeArticleViewStat where ParentId = < KnowledgeArticleId (from the KnowledgeArticleVersion object > where Channel = 'App'  --Hope that makes sense. KB