Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Query Most Viewed Knowledge articles and retrieve records only visible to User

gaurangpatel
Kilo Explorer

Hi

I want to display Most viewed knowledge Articles from a Knowledge base published with in last two days. I also want to make sure that The list of article which I display user must have access to display those articles.

I am not able to achive this by using SNC.knowledgeHelper().getMostViewedArticles

The other problem is for Admin users i want to display all published plus unpublished(draft, rejected retired etc) articles as well.

Any guidance would be helpful.

1 REPLY 1

Brent Sutton
Mega Sage

Hi Gaurang,



You can get the most viewed published articles from the previous 2 days by using GlideAggregate to query the kb_use table. This will perform a count on the number of times a newly published article has been viewed. I've provided some example code below that you can modify for your own purposes: -



getMostViewedArticles()



/****** return the most viewed published articles from the previous two days *****/


function getMostViewedArticles() {


     


      var maxEntries = 5; //change the maximum returned entries to whatever number you want


     


      var items = [];


     


      var count = new GlideAggregate('kb_use');


      //limit results to articles published on the previous two days.


      count.addEncodedQuery('article.publishedRELATIVEGE@dayofweek@ago@2');


      count.addAggregate('COUNT','article');


      count.groupBy('article');


      count.orderByAggregate('COUNT','article');


      count.query();


     


      var i = 0;


     


      //restrict number of articles based on maxEntries


      while (count.next() && count.canRead() && items.length < maxEntries) {


     


      //Extract whatever information you want from the article record.


      //You could also return the whole record if you want.


      var item = {};


              item.short_description = count.article.short_description.getDisplayValue();


              item.kb_category = count.article.kb_category.getDisplayValue();


              item.sys_view_count = count.getAggregate('count', 'article');


              item.published = count.article.published.getDisplayValue();


              item.sys_id = count.article.sys_id.getDisplayValue();


              items.push(item);


              i++;


      }


return items;


}



You really need to split your second question into its own thread. In the meantime I hope the above helps.



Brent