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

marcguy
ServiceNow Employee
ServiceNow Employee

Yes I had to do the actual KB Import script, on an instance running winter2009.

Then export xml from the older instance and import xml onto the final target instance.

If your going to knowledge 11 it may be worth asking someone from development on any plans to develop an easier Knowledge base import procedure (the problem is everybody stores their old knowledge base differently).

thanks,
Marc


poornima2
Mega Expert

Hi Marc,
We converted the word document to html using the 'word cleaner' tool and uploaded the both the image file and the html file using the 'upload file' module and ran the scheduled job.We have seen that the files have been converted as knowledge article, but the images were not visisble.What we doing wrong?
Thanks.....


poornima2
Mega Expert

Marc,
We found what we were doing wrong. It worked great.Thank you very much for your help.
Thanks


poornima2
Mega Expert

We got the following script(modified) from service-now and this worked great in our newer versions.
And the tool ("Word Cleaner (http://www.convertwordtohtml.com)") Marc referenced is very helpful and this tool has an option to embed images directly to our HTML file so that we dont need separate image files.This is really great ,we dont need to upload the html file and the imag file separately.

var DIRECTORY = '/glide/instances/lathamtest_16012/webapps/glide/itil/WEB-INF/update/customer/uploads/';
var IMAGE_PREFIX = 'rel="lightbox" src="https://www.service-now.com/lathamtest/scs/';

doImport(DIRECTORY);
function doImport(directory) {
var file = new Packages.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('.html');
if ((fileName.length() - test) == 5) {
gs.log('Processing file ' + fileName);
importOneFile(directory + fileName);
} else {
gs.log('Skipping file ' + fileName);
}
}
}
function importOneFile(fileName) {
var file = new Packages.java.io.File(fileName);
if (!file.exists()){
return;
}
if (!file.isFile()){
return;
}
var fileIS = new Packages.java.io.FileInputStream(file);
var reader = new Packages.java.io.InputStreamReader(fileIS);
var fileBA = new Packages.java.io.ByteArrayOutputStream();
var ch = null;
var total = "";
while (true)
{
ch = reader.read();
if (ch == -1)
{
break;
}
fileBA.write(ch);
}
total += fileBA.toString();
gs.print("TOTAL: " + total);
total = postProcess(total);
var kb = new GlideRecord('kb_knowledge');
kb.initialize();
kb.text = total;
var pos = fileName.lastIndexOf('/');
var kbname = fileName.substr(pos + 1);
kb.short_description = kbname;
kb.insert();
}
function postProcess(total)
{
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;
}

Thanks....


marcguy
ServiceNow Employee
ServiceNow Employee

Pleased that I could help, the option to embed the images is a great timesaver too, well spotted. Marc