KB Most Viewed widget tweak to display from multiple knowledge bases

wbwb
Tera Expert

Does anyone know how to update the existing KB Most Viewed Articles widget's code to pull from multiple knowledge bases?  Using Jakarta.

 

(function() {
 data.articles = [];
 options.title = options.title || gs.getMessage("Most Viewed Articles");
 var z = $sp.getKBRecord();
 z.addQuery("sys_view_count", ">", "0");
 if (options.kb_category)
  z.addQuery("kb_category", 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

Mark Stanger
Giga Sage

I don't have an easy solution for you with a from-scratch widget, but there is a pretty simple solution using a widget that ServiceNow has already built.

The Knowledge Management Service Portal plugin improves significantly upon the Service Portal KM capabilities...particularly around handling multiple knowledge base scenarios like this.  It's included in Jakarta and is easy to activate and use.  You should be able to turn it on and just use their enhanced widget to solve your problem if you wanted without impacting your existing service portal.

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/product/knowledge-management/concept/knowledge-management-service-portal.html

'Knowledge MostViewed Articles' is the name of the widget you'll want to use once you've activated the plugin.  It includes most viewed articles from all Knowledge Bases by default, but you can restrict it with a simple option change to whatever KBs you want.

You can get a bunch of really nice additional functionality if you fully integrate the new portal into your existing portal though so you might want to check that out as well.  Here are the instructions for integrating the 2 portals.

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/product/knowledge-management/concept/conf-service-portal-know-management.html

View solution in original post

6 REPLIES 6

Mark Stanger
Giga Sage

I don't have an easy solution for you with a from-scratch widget, but there is a pretty simple solution using a widget that ServiceNow has already built.

The Knowledge Management Service Portal plugin improves significantly upon the Service Portal KM capabilities...particularly around handling multiple knowledge base scenarios like this.  It's included in Jakarta and is easy to activate and use.  You should be able to turn it on and just use their enhanced widget to solve your problem if you wanted without impacting your existing service portal.

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/product/knowledge-management/concept/knowledge-management-service-portal.html

'Knowledge MostViewed Articles' is the name of the widget you'll want to use once you've activated the plugin.  It includes most viewed articles from all Knowledge Bases by default, but you can restrict it with a simple option change to whatever KBs you want.

You can get a bunch of really nice additional functionality if you fully integrate the new portal into your existing portal though so you might want to check that out as well.  Here are the instructions for integrating the 2 portals.

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/product/knowledge-management/concept/conf-service-portal-know-management.html

Having said that, you might be able to simply change this line in your script above...

var z = $sp.getKBRecord();

to this...

var z = new GlideRecord('kb_knowledge');

z.addQuery('kb_knowledge_base', 'yourknowledgebasesysid');

 

You could also adjust your options schema similar to ask for the knowledge base similar to the way it asks for kb category to make it more dynamic.

Hi Mark

 

Thanks for this suggestion, it was really helpful for us also.

The problem we are facing now is that when we use the default "Knowledge MostViewed Articles" widget it first runs against all available Knowledge Bases and returns the top 5 and then it hides the articles that you don't have access to.

This is a problem for us because some of our most viewed articles aren't visible for all users therefore the widget displays as blank for them.

Can you or anyone else contributing here think of a way to first filter the Knowledge Articles by what is available to the user and then list the top 5 of those?

 

//Tom

Hello Mark,

Thanks for this information.  I believe this is what I need as well.  However I have tried to add just the Knowledge Bases I need to show the articles without success.  I believe it is something on the way I am writing the options but cannot find how to do it.  I will greatly appreciate if you can show me what is it that is wrong on my code. Here is the code with the line changed (line 9) to point to the KB I need.

 

(function() {

    options.title = options.title || gs.getMessage('Most Viewed');
    options.display_field = options.display_field || "short_description";
    options.secondary_fields = options.secondary_fields ? options.secondary_fields.split(",") : getKnowledgeFieldsfromProperties();
    options.show_secondary_fields_label = options.show_secondary_fields_label ?        options.show_secondary_fields_label == 'true' : false;
    options.max_records = options.max_records || gs.getProperty('glide.knowman.content_block_limit')|| '5';
    options.always_show = options.always_show ? options.always_show == 'true' : true;
    options.knowledge_base =  (options.knowledge_base || "IT" || "Knowledge"); // ***this is how I am trying to point to the KB*****
    //options.knowledge_base =  options.knowledge_base || ""; //original line
    options.reverse = true;


    var kbService = new KBPortalService();
    var result = [];

    var kbData = kbService.getMostViewedArticles(options.max_records,options.display_field,options.secondary_fields,options.knowledge_base);
    if(kbData)
        result= kbData;

    options.result = result;
    data.template = $sp.getWidget("kb-list-widget-template", options);


    function getKnowledgeFieldsfromProperties(){

        //Generate secondary fields based on legacy properties
        var fields = [];

        if(gs.getProperty('glide.knowman.search.show_article_number') && gs.getProperty('glide.knowman.search.show_article_number') == 'true'){
            var kbMod = new global.KBViewModel();
            if(kbMod.isVersioningEnabled()){
                fields.push('display_number');
            }else{
                fields.push('number');
            }
        }
        if(gs.getProperty('glide.knowman.search.show_author') && gs.getProperty('glide.knowman.search.show_author') == 'true')
            fields.push('author');
        if(gs.getProperty('glide.knowman.search.show_view_count') && gs.getProperty('glide.knowman.search.show_view_count') == 'true')
            fields.push('sys_view_count');        
        if(gs.getProperty('glide.knowman.search.show_last_modified') && gs.getProperty('glide.knowman.search.show_last_modified') == 'true')
            fields.push('sys_updated_on');
        if(gs.getProperty('glide.knowman.search.show_published') && gs.getProperty('glide.knowman.search.show_published') == 'true')
            fields.push('published');
        if(gs.getProperty('glide.knowman.show_unpublished') && gs.getProperty('glide.knowman.show_unpublished') == 'true'){
            fields.push('workflow_state');
        }

        fields.push('rating');

        return fields;
    }
})();