I want to get a list of knowledge that users can view

Yuto Nakayama1
Tera Contributor

Is there a way to get a list of knowledge (such as sys_id) that can be viewed by logged in users?

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Yuto Nakayama1 

If you'd like to check with the ACLs, you can use the API canRead().

canRead(): Determines if the Access Control Rules (ACLs) permit reading records in this table. This method evaluates all ACL types, such as user roles, scripted ACLs, ACLs with scripted conditions, and so on.

Sample below.

var articleSysID = '064b8f0b47e63d90ab9bb6bf016d434d'; //Replace the Article sys_id
var userSysID = '165bded5477a79d0ab9bb6bf016d43c7'; //Replace the User sys_id

var grKnowledge = new GlideRecord('kb_knowledge');
grKnowledge.get(articleSysID);
gs.getSession().impersonate(userSysID);
gs.log(grKnowledge.canRead());

 

And if you'd like to verify with the User Criteria, you can leverage the script include KBDiagnostics and the getDiagnostics function.

Sample below.

var articleSysID = '064b8f0b47e63d90ab9bb6bf016d434d'; //Replace the Article sys_id
var userSysID = '165bded5477a79d0ab9bb6bf016d43c7'; //Replace the User sys_id

//User Criteria
var diagnose = new KBDiagnostics(true, userSysID, articleSysID);
var result = diagnose.getDiagnostics();
gs.log(result.summary_header);
gs.log(result.note);

Screenshot 2023-12-18 at 18.07.44.png

 

Cheers,

Tai Vu

View solution in original post

4 REPLIES 4

Ratnakar7
Mega Sage
Mega Sage

Hi @Yuto Nakayama1 ,

 

In ServiceNow, accessing the list of knowledge articles that users can view can be achieved using several approaches. One common way is to query the kb_knowledge table. Here's a script you can use in a background script or a script include to get a list of knowledge articles that are published and can be viewed:


var gr = new GlideRecord('kb_knowledge');
// Add conditions to filter only published articles
gr.addQuery('workflow_state', 'published');
// Add additional conditions if needed, such as category, etc.
// gr.addQuery('category', 'your_category');

gr.query();

while (gr.next()) {
    // Access information about each knowledge article
    var sysId = gr.getValue('sys_id');
    var title = gr.getValue('title');
    
    // Do something with the information, e.g., print to the console
    gs.info('Knowledge Article: Sys ID - ' + sysId + ', Title - ' + title);
}

 

Thanks,

Ratnakar

Thank you for your reply!
If the knowledge that can be displayed is restricted by user criteria, is it possible to list only the knowledge that can be displayed?

Tai Vu
Kilo Patron
Kilo Patron

Hi @Yuto Nakayama1 

If you'd like to check with the ACLs, you can use the API canRead().

canRead(): Determines if the Access Control Rules (ACLs) permit reading records in this table. This method evaluates all ACL types, such as user roles, scripted ACLs, ACLs with scripted conditions, and so on.

Sample below.

var articleSysID = '064b8f0b47e63d90ab9bb6bf016d434d'; //Replace the Article sys_id
var userSysID = '165bded5477a79d0ab9bb6bf016d43c7'; //Replace the User sys_id

var grKnowledge = new GlideRecord('kb_knowledge');
grKnowledge.get(articleSysID);
gs.getSession().impersonate(userSysID);
gs.log(grKnowledge.canRead());

 

And if you'd like to verify with the User Criteria, you can leverage the script include KBDiagnostics and the getDiagnostics function.

Sample below.

var articleSysID = '064b8f0b47e63d90ab9bb6bf016d434d'; //Replace the Article sys_id
var userSysID = '165bded5477a79d0ab9bb6bf016d43c7'; //Replace the User sys_id

//User Criteria
var diagnose = new KBDiagnostics(true, userSysID, articleSysID);
var result = diagnose.getDiagnostics();
gs.log(result.summary_header);
gs.log(result.note);

Screenshot 2023-12-18 at 18.07.44.png

 

Cheers,

Tai Vu

I achieved my goal!
Thank you!