Knowledge Base Category Subscription Widget
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 02:18 AM
I am currently working on creating a widget for the csm portal. The use case is to build a widget that will display all Knowledge bases and their categories allowing the user to subscribe to a specific knowledge base category (if they have read access to the knowledge base).
The following server script allows me to achieve this use case but I have been asked to further restrict the knowledge bases returned based on the service portal the knowledge bases are associated with. For example, I need to only return knowledge bases that are associated with the CSM portal. Can anyone help?
(function() {
// Initialize the data structure to hold knowledge bases
data.knowledgeBases = [];
// Create a GlideRecord object for the kb_knowledge_base table
var kbGR = new GlideRecord('kb_knowledge_base');
if (!kbGR.isValid()) {
gs.info('Table kb_knowledge_base not found or inaccessible.');
return;
}
// Query all records in the kb_knowledge_base table
kbGR.query();
gs.info('kb_knowledge_base records found: ' + kbGR.getRowCount());
// Get current user's ID
var userId = gs.getUserID();
// Loop through each knowledge base record
while (kbGR.next()) {
// Check if the current user has access to this knowledge base
if (!kbGR.canRead()) {
gs.info('User does not have access to KB: ' + kbGR.sys_id);
continue; // Skip this knowledge base if the user cannot read it
}
// Retrieve the title field instead of name
var kbTitle = kbGR.title ? kbGR.title.toString() : "Unnamed Knowledge Base";
// Create an object for each knowledge base with its id and title
var kb = {
id: kbGR.sys_id.toString(),
name: kbTitle, // Using 'title' now instead of 'name'
categories: []
};
gs.info('Processing Knowledge Base: ' + kb.id + ' - ' + kb.name); // Log each KB to verify title retrieval
// Fetch categories associated with this knowledge base
var catGR = new GlideRecord('kb_category');
if (!catGR.isValid()) {
gs.info('Table kb_category not found or inaccessible.');
return;
}
// Adjust the query if the field is not 'parent_id'
catGR.addQuery('parent_id', kbGR.sys_id); // or 'knowledge_base' if 'parent_id' is incorrect
catGR.query();
gs.info('Categories found for KB ' + kbGR.sys_id + ': ' + catGR.getRowCount());
// Loop through each category record for the current knowledge base
while (catGR.next()) {
// Check subscription status for the current category
var isSubscribed = false; // Default value
// Query the subscription table to see if the user is subscribed to the category
var subGR = new GlideRecord('u_knowledge_base_category_subscription');
subGR.addQuery('u_category', catGR.sys_id);
subGR.addQuery('u_user', userId);
subGR.query();
if (subGR.hasNext()) {
isSubscribed = true; // User is subscribed if a record exists
}
kb.categories.push({
id: catGR.sys_id.toString(),
name: catGR.label ? catGR.label.toString() : "Unnamed Category", // Using 'label' for category name
isSubscribed: isSubscribed // Set the subscription status
});
}
// Only add the processed knowledge base if it has categories
if (kb.categories.length > 0) {
data.knowledgeBases.push(kb);
}
}
// Final log to confirm total knowledge bases processed
gs.info('Total Knowledge Bases processed: ' + data.knowledgeBases.length);
})();
0 REPLIES 0