Import a knowledge base from static html files

poornima2
Mega Expert

We would like to import an existing KB article we have saved locally as an HTML file to the Service-Now server and create a new KB.
I was provided a script which is in the wiki (http://wiki.service-now.com/index.php?title=Useful_Scripts#Import_a_knowledge_base_from_static_html_files_.26_rename_all_image_tags). This script is supposed to enable uploading of HTML files(including images) to the KB.
My problem is,i can able to upload the file into the KB,but the images are not visible in the KB article
(The images within the articles are not uploading).If anyone else has had success with such an operation, please let us know.

Thanks.

22 REPLIES 22

CapaJC
ServiceNow Employee
ServiceNow Employee

Don't think there's an easy way to do this yet. I know that script in Useful Scripts was specifically written for a specific customer, and posted to the wiki article as-is, and things were set up very specifically to make it work for that customer. It was kinda hacky and took a lot of time, if I recall.

I also know the developer who did it said he'd never do it again, unless there was a simple and easy-to-use method for HTML-to-Knowledge Base imports. As far as I know, there isn't yet.

I tihnk the images and HTML files for this example were all stored locally on a hard drive for this import.


Hi all,

Its been more than 2 years since the post, has anyone had any new and useful experiences mass importing KnowledgeBase articles into Service-Now?

We're migrating from HP ServiceCentre (6.x). We have rich html articles with inline screenshots and various non-image attachments. Transitioning the content will prove challenging; and to do it in an automated fashion.

Quick question, when processing the files using java.io.File(), are these files placed on the server-side instance via Service-Now staff? How else would you get them onto the raw file system? there is no ftp access etc?

Thanks

Pete.


ethal
Kilo Contributor

I am a co-worker of Poonima's (author of post). Actually, we have been working with this script for well over a year. We finally got very close to achieving our goal, and inquired with Service-Now's technical support for assistance ironing out the last detail, and were asked to post the question on this forum. We are not trying to administer the aforementioned script as-is - we have modified it to suit our needs. We are now able to successfully import html files (articles) to the knowledge base using the scheduled job feature, we just have not been successful importing associated image files. Below is the the script. The first two lines are the portion which I believe focuses on what we are attempting to achieve. It has been significantly modified from the original version posted on the wiki.

var DIRECTORY = '/glide/instances/lathamqa_16004/webapps/glide/itil/WEB-INF/update/customer/uploads/'; //service-now gave this path

var IMAGE_PREFIX = 'src="https://www.service-now.com/lathamqa/scs/';

doImport(DIRECTORY);
function doImport(directory) {
var file = new java.io.File(directory);

if (!file.exists()) {
gs.log('Could not execute import from directory ' + directory + ' as it does not exist.');
return;
}

if (!file.isDirectory()) {
gs.log('Could not execute import from directory ' + directory + ' as it is not a directory.');
return;
}

var contents = file.list();
for (var x =0; x< contents.length; x++) {
var fileName = contents[x];
var test = fileName.lastIndexOf('.htm');
if ((fileName.length() - test) == 4) {
// gs.log('Processing file ' + fileName);
importOneFile(directory + fileName);
} else {
//gs.log('Skipping file ' + fileName);
}
}
}

function importOneFile(fileName) {
var file = new java.io.File(fileName);
if (!file.exists()){
return;
}
if (!file.isFile()){
return;
}
var fileReader = new java.io.FileReader(file);
var reader = new java.io.BufferedReader(fileReader);
var l = null;
var total = "";
while (true) {
l = reader.readLine();
if (l == null){
break;
}
total = total + l;

}
total = postProcess(total);
var kb = new GlideRecord('kb_knowledge');
kb.initialize();
kb.text = total;
var pos = fileName.lastIndexOf('/'); //newly added line
var kbname = fileName.substr(pos+1); //newly added line
kb.short_description = kbname;
kb.insert();
}
function postProcess(total) { //problem in this postprocess function
if (total == null){
return null;
}
var exp = new RegExp("src=\"../https://", "g");
var out = total.replace(exp, IMAGE_PREFIX);
var index = out.indexOf(IMAGE_PREFIX, index);

while (index >=0) {
var endIndex = out.indexOf('"', IMAGE_PREFIX.length + index);
var path = out.substring(index, endIndex);
gs.log('SUBSTRING = ' + path);
var fixedPath = path.toLowerCase();
gs.log('FIXED PATH = ' + fixedPath);
out = out.replace(path, fixedPath);
index = out.indexOf(IMAGE_PREFIX, endIndex);
gs.log('END INDEX = ' + index);
}
return out;
}

For the record, we attempted to have this work contracted to Service-Now and would have been happy to pay for assistance, but the issue did not receive attention from Service-Now after numerous inquiries. That seems to have worked out for the best as we have had to tackle it on a deeper level and have come away learning more about Javascript and the Service-Now ITSM suite.

Any assistance would be greatly appreciated.


poornima2
Mega Expert

Has anyone else experienced this same issue?