- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2023 10:21 PM
Is there a way to get a list of knowledge (such as sys_id) that can be viewed by logged in users?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2023 03:08 AM
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);
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2023 10:52 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2023 01:31 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2023 03:08 AM
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);
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2023 06:55 PM
I achieved my goal!
Thank you!