+ Start a Discussion
di_zoudi_zou 

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

I have a VisualForce Component that looks like this:

 

<ul>
    <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">
            <tr>
            <td>{!article.articlenumber}</td>
            <td><a target="_parent" href="{!URLFOR($Action.KnowledgeArticle.View, article.id)}">{!article.title}</a></td>
            <td>{!article.articletypelabel}</td>
            <td>{!article.lastpublisheddate}</td>
            </tr>
            
        </knowledge:articleList>
        </table>
    </ul>

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

francoisLfrancoisL

"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.

di_zoudi_zou

How would I do that in the VisualForce component?

di_zoudi_zou

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 />
</apex:component>

 

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">
            </apex:repeat>
        </c:Query_component>
    </knowledge:articleList>
</apex:page>

 

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

francoisLfrancoisL

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

di_zoudi_zou

KnowledgeArticleViewStat uses ParentId to reference KnowledgeArticle, so I changed:

 

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

 

to:

 

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

 

Based on this:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_guidelines_knowledge.htm

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

 

Is my apex code for retrieving the value correct?

RishavRishav
Hiii,
      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 
Thanks 
Rishav kumar
KB1618KB1618
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