How to limit most viewed articles for a specific knowledge base

lrossy31
Tera Expert

Hello there again,

Wanted to ask if anyone has an idea to get the most viewed articles limit to specific knowledge bases.  I am using the knowledge blocks limiting those to the ones I want to show and I have them on the main page.  When I click any of the blocks it will take me to the page sp2?id=kb_view2&kb=sys_id, which I have the kb_category_blocks that will show only the categories for the knowledge base I just selected.  Also I have there the most viewed articles but it shows the articles for all knowledge bases.  I tried to manipulate the server script on the widget with a suggestion from Mark changing the lines noted below.

When the page sp2?id=kb_view2&kb=sys_id shows the correct categories for the knowledge base.  Again, I will greatly appreciate the collaboration on this.

 

(function() {
    data.articles = [];
    options.title = options.title || gs.getMessage("Most Viewed Articles");
    //var z = $sp.getKBRecord();

    var z = new GlideRecord('kb_knowledge'); // changed line


    z.addQuery("sys_view_count", ">", "0");
   // if (options.kb_category)

      if (options.kb_knowledge_base) //changed if
        z.addQuery("kb_knowledge_base", options.kb_knowledge_base); //this was kb_category and options.kb_category
    z.orderByDesc('sys_view_count');
    z.setLimit(options.max_number || 5);
    z.query();
    while (z.next()) {
        if (!z.canRead())
            continue;

        var a = {};
        $sp.getRecordValues(a, z, 'short_description,sys_view_count,sys_id,published');
        data.articles.push(a);
    }
})()

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Try the below script

(function() {
	data.articles = [];
	options.title = options.title || gs.getMessage("Most Viewed Articles");
	//var z = $sp.getKBRecord();
	
	data.kb_knowledge_base = options.kb_knowledge_base || $sp.getParameter("kb");
	
	var z = new GlideRecord('kb_knowledge'); // changed line
	
	
	z.addQuery("sys_view_count", ">", "0");
	// if (options.kb_category)
	
	if (data.kb_knowledge_base) //changed if
		z.addQuery("kb_knowledge_base", data.kb_knowledge_base); //this was kb_category and options.kb_category
	z.orderByDesc('sys_view_count');
	z.setLimit(options.max_number || 5);
	z.query();
	while (z.next()) {
		if (!z.canRead())
			continue;
		
		var a = {};
			$sp.getRecordValues(a, z, 'short_description,sys_view_count,sys_id,published');
			data.articles.push(a);
		}
	})()

View solution in original post

3 REPLIES 3

dvp
Mega Sage
Mega Sage

Try the below script

(function() {
	data.articles = [];
	options.title = options.title || gs.getMessage("Most Viewed Articles");
	//var z = $sp.getKBRecord();
	
	data.kb_knowledge_base = options.kb_knowledge_base || $sp.getParameter("kb");
	
	var z = new GlideRecord('kb_knowledge'); // changed line
	
	
	z.addQuery("sys_view_count", ">", "0");
	// if (options.kb_category)
	
	if (data.kb_knowledge_base) //changed if
		z.addQuery("kb_knowledge_base", data.kb_knowledge_base); //this was kb_category and options.kb_category
	z.orderByDesc('sys_view_count');
	z.setLimit(options.max_number || 5);
	z.query();
	while (z.next()) {
		if (!z.canRead())
			continue;
		
		var a = {};
			$sp.getRecordValues(a, z, 'short_description,sys_view_count,sys_id,published');
			data.articles.push(a);
		}
	})()

This worked great!  Thanks a lot!

DScroggins
Kilo Sage

Im going to assume in your edited widget you have added an option schema for options.kb_knowledge_base? And then in the Widget Instance for the Most View Articles you have the option set to the correct Knowledge Base you want to view the most viewed articles for?

 

If you don't want the Knowledge Base to come from a preset option then you can always use $sp.getParameter(PARAM NAME) if the sys id the KB is being passed in the URL.