How to filter 'Knowledge Bases Browse' widget results based on a custom field on the kb_knowledge_base table

sarah_kovar
Tera Contributor

 

I created a new field on the kb_knowledge_base table called u_type that I want to be able to use within Service Portal to limit knowledge search results depending on which portal page I'm on. For the purposes of this post, I am specifically wondering how to add the query to the cloned, OOB widget 'Knowledge Bases Browse' Server Script. I'd like to pull back knowledge bases I have access to, then if I'm on the 'Manuals' knowledge page and have a Manuals specific Knowledge Bases Browse widget, only the knowledge bases defined with a u_type of 'manual' are displayed.  

The OOB Server script is:

(function() {
if(input){
var status = "failed";
if(input.id && input.action){
if(input.action == "subscribe"){
subscribeKB(input.id);
status ="success";
}else if(input.action == "unsubscribe"){
unSubscribeKB(input.id);
status ="success";
}
}
data.status = status;
}else{
data.sqaUiActive = GlidePluginManager.isActive('com.snc.knowledge.social_qa.ui');
options.order_by = options.order_by ? options.order_by : "title";
options.order_reverse = options.order_reverse ? options.order_reverse == 'true': false;
data.instanceid = $sp.getDisplayValue("sys_id");
data.kb_label = gs.getMessage('knowledge base {0} having {1} articles');
data.kb_label_qa = gs.getMessage('knowledge base {0} having {1} articles and {2} questions');

var kbService = new KBPortalService();
options.knowledge_bases = options.knowledge_bases || String(kbService.getServicePortalKnowledgeBases($sp.getPortalRecord().url_suffix)) || "";
data.isMobile = kbService.isMobile();
data.result = kbService.getMyKnowledgeBases(options.order_by,options.knowledge_bases);
var kbCount = data.result.length;
var articleCount = 0;
var socailqaCount = 0;

data.result.forEach(function(key){
articleCount = articleCount + parseInt(key.article_count,10);
if (data.sqaUiActive)
socailqaCount = socailqaCount + parseInt(key.questions_count,10);

});

var canSuscribe = false;
var canCreateArticle = false;
var canPostQuestion = false;
var kbService2 = new KBPortalService();
canSuscribe = kbService2.canSubscribe();
canCreateArticle = kbService2.canCreateArticle(options.knowledge_bases);
canPostQuestion = kbService2.canPostQuestion(options.knowledge_bases);

data.total_kb_count = kbCount;
data.total_articles_count = articleCount;
if (data.sqaUiActive)
data.total_socialqa_count = socailqaCount;
data.canSuscribe = canSuscribe;
data.canCreateArticle = canCreateArticle;
data.canPostQuestion = canPostQuestion;
data.subscribeText = gs.getMessage("Subscribe to knowledge base {0}")
data.unsubscribeText = gs.getMessage("Unsubscribe from knowledge base {0}");
}

function subscribeKB(kbID){
var context = global.ActivitySubscriptionContext.getContext();
context.getSubscriptionService().subscribe("722d67c367003200d358bb2d07415a9c",kbID);
}

function unSubscribeKB(kbID){
var context = global.ActivitySubscriptionContext.getContext();
context.getSubscriptionService().unsubscribe(kbID);
}

})();

4 REPLIES 4

Aaditya Gupta
ServiceNow Employee
ServiceNow Employee

Hi @sarah.kovar 

This requirement can be acheived using getServicePortalKnowledgeBases method, this method takes in the portal suffix as method argument and try to find the knowledge bases defined in the m2m_sp_portal_knowledge_base table corresponding to the portal specified. 

To achieve this requirement you can just create records in m2m_sp_portal_knowledge_base for the each portal.

 

Please mark this answer as correct and helpful if this helped in resolving you query.

 

Thanks & regards

Aaditya Gupta

Hi @Aaditya Gupta, thanks for the response!  We have entries in the m2m_sp_portal_knowledge_base. Would you be able to give me an example of how to use getServicePortalKnowledgeBases to limit to the custom field on the knowledge base table called u_type? 

I see it being used here within the widget's server script: options.knowledge_bases = options.knowledge_bases || String(kbService.getServicePortalKnowledgeBases($sp.getPortalRecord().url_suffix)) || "";

And, I also see how the script is querying to obtain getMyKnowledgeBases; however, I'm not sure how to limit the results to only those with a u_type="manual" specified. 

HI @sarah.kovar 

 

Okay got it, your requirement is not limited to the portal but to a field, Then i think you might need to filter the knowledge base ids (Using Gliderecord) and put the values of in options.knowledge_bases on server side. 

 

Thanks & regards

Aaditya Gupta

lvidal
Tera Contributor

Did you find a solution excuse me?