How to replicate the Tree structure of Knowledge Categories in a SP widget

sudharsanv36
Kilo Guru

Hi All,

We are trying to replicate the native functionality of Knowledge articles, which will group the KB by categories.

You can find the similar functionality at <instance_name>.service-now.com/$knowledge.do

find_real_file.png

The same functionality has to be built in a widget and use that in the Service Portal, Kindly let me know if anybody achieved this.

1 ACCEPTED SOLUTION

larstange
Mega Sage

Actually the code will support any number of sub levels - I have never tried it with more than 3 levels though


View solution in original post

13 REPLIES 13

Hei Lars!



I've downloaded your share app. and trying to edit the widget to suit knowledgebase.   I struggle with the parent part.   for service catalog, parent is empty on top , while in knowledge category, the parent_id has the knowledgebase as parent_id   (and we even have several knowledge bases to complicate it).   How would the parent query be in the widget code:   assuming i have to edit this part:



kb uses parent_id instead of parent



var cat = {};


      cat.title = sc.label.getDisplayValue();


      cat.open = isSubCat(sc.sys_id,data.category_id) || data.category_id == sc.sys_id;


      cat.sys_id = sc.getUniqueValue();


  //cat.isParent = isParent(sc);



if (sc.getValue('parent'))


  cat.parent = sc.getValue('parent');


  else


  cat.parent = "top";



if (!data.categories[cat.parent])


  data.categories[cat.parent] = [];


  data.categories[cat.parent].push(cat);


  }


function isSubCat(cat,subcat) { //Check if a category is a subcategory to another


  if (!subcat)


  return false;



  var sc = new GlideRecord('kb_category');



  //Get our subcategory


  if (sc.get(subcat)) {


  //See if the main category is one of our parents


  if (JSUtil.notNil(sc.parent_id)) {


  if (sc.parent == cat)


  return true;


  return isSubCat(cat,sc.parent);


  }


  return false;


  }


  }


function getSubCategories(category) {


  //find subcategories


  var arr = [];


  var sub = new GlideRecord('kb_category');


  sub.addQuery('parent', category.sys_id);


  sub.addActiveQuery();


  sub.orderBy('label');


  sub.query();


  while (sub.next()) {


  if (!$sp.canReadRecord("kb_category", sub.getUniqueValue()))


  continue;


  var subcategory = {};


  subcategory.sys_id = sub.getUniqueValue();


  subcategory.title = sub.label.getDisplayValue();


  subcategory.subcategories = [];


  subcategory.subcategories = getSubCategories(sub); //Find further subcategories


  subcategory.open = isSubCat(sub.sys_id,data.category_id);


  arr.push(subcategory);


  }


  return arr;


  }



Hey Ingrid,



Just curious as to your progress on this...we to would love to have a modified widget that displays the subcategories on the KB.


Thanks,


ML


Hi Marc! sorry for late reply.   anyway, no sorry didn't get any way on this . was hoping for some help here, since I am no scripting expert.


THANK YOU for making this! This saved us so much time and headache!!


ingaro
Tera Expert

Hi Sudharsan!   did you manage to replicate the tree structure to service Portal?