featured content Knowledge article sort order in service portal

sai krishna10
Giga Guru

Hi,

 

I Need to sort the order of featured content Knowledge article based on sys_updated_on in serviceportal. I am using the OOB Widget. Can anyone let me know where I need to modify the serviceportal script

(function() {

	options.title = options.title || gs.getMessage('Featured');
	options.display_field = options.display_field || "short_description"; 
	options.secondary_fields = options.secondary_fields ? options.secondary_fields.split(",") : getKnowledgeFieldsfromProperties();
	options.show_secondary_fields_label = options.show_secondary_fields_label ? options.show_secondary_fields_label == 'true' : false;
	options.max_records = options.max_records || gs.getProperty('glide.knowman.content_block_limit')|| '5';
	options.always_show = options.always_show ? options.always_show == 'true' : true;
	options.knowledge_base = options.knowledge_base || "";
	options.reverse = false;

	if(options.display_field != "short_description"){
		options.secondary_fields.push(options.display_field);
	}

	var kbService = new KBPortalService();
	var j = 0;
	var result = [];
	var kb = kbService.getFeaturedArticles(options.max_records,options.secondary_fields);
	if(kb.results){
		kb.results.forEach(function(f){
			var record = {};
			record.id = f.id+"";
			record.link = f.link;
			record.direct = f.meta.direct || false;
			record.external = f.meta.external || false;
			record.external_link = f.meta.external_link;

			if (options.display_field){
				if(options.display_field == "short_description"){
					record.display_field = f.title;
				}else{
					record.display_field = f.meta[options.display_field].display_value;
				}
			}
			record.order = j;
			j++;

			record.secondary_fields = [];
			options.secondary_fields.forEach(function(key){
				if(options.display_field != key)
				record.secondary_fields.push(f.meta[key]);
			});


			result.push(record);
		});
	}

	options.result = result;
	data.template = $sp.getWidget("kb-list-widget-template", options);

	function getKnowledgeFieldsfromProperties(){

		//Generate secondary fields based on legacy properties
		var fields = [];

		if(gs.getProperty('glide.knowman.search.show_article_number') && gs.getProperty('glide.knowman.search.show_article_number') == 'true'){
			var kbMod = new global.KBViewModel();
			if(kbMod.isVersioningEnabled()){
				fields.push('display_number');
			}else{
				fields.push('number');
			}
		}
		if(gs.getProperty('glide.knowman.search.show_author') && gs.getProperty('glide.knowman.search.show_author') == 'true')
			fields.push('author');
		if(gs.getProperty('glide.knowman.search.show_view_count') && gs.getProperty('glide.knowman.search.show_view_count') == 'true')
			fields.push('sys_view_count');		
		if(gs.getProperty('glide.knowman.search.show_last_modified') && gs.getProperty('glide.knowman.search.show_last_modified') == 'true')
			fields.push('sys_updated_on');
		if(gs.getProperty('glide.knowman.search.show_published') && gs.getProperty('glide.knowman.search.show_published') == 'true')
			fields.push('published');
		if(gs.getProperty('glide.knowman.show_unpublished') && gs.getProperty('glide.knowman.show_unpublished') == 'true'){
			fields.push('workflow_state');
		}

		fields.push('rating');

		return fields;
	}
})();

 

Thanks

Sai Krishna

6 REPLIES 6

Varsha21
Giga Guru

Hello Sai Krishna,

did you get the solutiuon for same?i have the same requirement.

 

Hi Varsha,

 

We didn't got the solution for this issue. We need to modify the widget script to sort this issue but we are unable to findout that.

Regards

Sai Krishna

Thank you Sai Krishna for the update,

 

Hello Community Experts,

 

 

Can you please assist me on the same?

 

Thanks

Varsha

 

 

Phonsie Hevey1
Tera Expert

I've managed to get this working after cloning the 2 widgets and updating the code as follows

Knowledge Featured Articles

Instead of ordering by an integer the order is done by the secondary_fields which in our case is just the sys_created_on field. So the value of record.order is set to this rather than the integer j.

j++;

			record.secondary_fields = [];
			options.secondary_fields.forEach(function(key){
				if(options.display_field != key)
				record.secondary_fields.push(f.meta[key]);
			});

			record.order = record.secondary_fields[0].value;
			

For this to work the function getKnowledgeFieldsfromProperties was also simplified just to be

function getKnowledgeFieldsfromProperties(){

	//Generate secondary fields based on legacy properties
	var fields = [];		
	fields.push('sys_created_on');
	return fields;
}

If you want to have more secondary fields you will need to extend the code to figure out which value in the secondary field array contains the data you want to sort on.

Knowledge List Widget Template

The Body HTML template by adding | orderBy:'-order' as shown below

<div ng-if="c.options.result.length > 0" ng-repeat="item in c.getOrderedItems() | orderBy:'-order' track by item.id" style="margin-bottom: 1em;">
		

 

I hope that helps. I couldn't see how the data was being pulled in terms of a GlideRecord where I would usually user to add the required order by.

Kind regards,

Phonsie.