Import Knowledge articles from confluence using Integration with external knowledge sources
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2021 11:22 AM
Has anyone Imported Knowledge articles from confluence using "Integration with external knowledge sources"?
https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/product/knowledge-management/concept/knowledge-external-content-integration.html
If so, any issues / tips/ experiences with that integration.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:41 PM
Why not 🙂 Sorry for variables names but i tried to keep them short.
Prerequisites: Admin access to confluence, ability in ServiceNow to create KB and Rest Messages.
Rest Message URL's
GET_Child | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}/child/page |
GET_Body | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}?expand=body.export_view |
GET_Author | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID} |
GET_Label | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}/label |
Attachment_list | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}/child/attachment |
Attachment_download | GET | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}/child/attachment/${attachmentID}/download |
POST_Label | POST | https://yourcompanyname.atlassian.net/wiki/rest/api/content/${confluenceID}/label |
First step is to create Before BR in kb_knowledge table with option Update. I added one more condition short_description contains upload_from_confluence. Then you need to create KB with this text in short description and each time you update this KB you will run this BR (this was the easiest way for me to check results).
Second step - create Rest Messages
Third step - get all child pages id from main page and saved them somewhere (e.g in notepad) Important! you need to run Get Child call few times to get all child id's
//genarate list of child page
var confluenceID; // put here id for main page
var t;
var r = new sn_ws.RESTMessageV2('Confluence', 'GET_Child');
r.setStringParameter('confluenceID', confluenceID);
var response = r.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var j = JSON.parse(responsebody);
for (var i=0;i<j.results.length;i++){
t+="'"+j.results[i].id +"',";
//t+= j.results[i].id +" "+j.results[i].title+"<p />";
}
}
current.text = t; //a.length;
current.update();
// End of child pages
next when you have pages id list comment above code and copy below one. Copy to array "a" pages id.
var confluenceID;
var mm;
var a = [];
gs.addInfoMessage("Count: " + a.length);
for(var b=0;b<a.length;b++){
confluenceID =a[b];
var r = new sn_ws.RESTMessageV2('Confluence', 'GET_Body');
r.setStringParameter('confluenceID', confluenceID);
var response = r.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var j = JSON.parse(responsebody);
var p = new sn_ws.RESTMessageV2('Confluence', 'GET_Author');
p.setStringParameter('confluenceID', confluenceID);
var response2 = p.execute();
var responsebody2 = response2.getBody();
var httpStatus2 = response2.getStatusCode();
var k = JSON.parse(responsebody2);
var y = new sn_ws.RESTMessageV2('Confluence', 'GET_Label');
y.setStringParameter('confluenceID', confluenceID);
var response5 = y.execute();
var responsebody5 = response5.getBody();
var httpStatus5 = response5.getStatusCode();
var m = JSON.parse(responsebody5);
mm = "";
for(var w=0;w<m.results.length;w++){
mm += m.results[w].name+",";
}
var meta;
if(mm.length>0){
meta=mm.slice(0,-1).replaceAll('undefined',"");
}
else{
meta=j.title;
}
var gr = new GlideRecord('kb_knowledge');
gr.initialize();
gr.short_description = j.title;
gr.kb_knowledge_base = 'sysid of KB base e.g. Knowledge';
gr.kb_category = 'sysid for KB category'; //I recomend to create new one for this import
gr.meta = meta;//j.title;
gr.text = j.body.export_view.value.replaceAll('class="wrapped relative-table confluenceTable"','class="wrapped relative-table confluenceTable" border="1"').replaceAll('class="wrapped tablesorter tablesorter-default confluenceTable"','class="wrapped tablesorter tablesorter-default confluenceTable" border="1"').replaceAll('<table class="wrapped confluenceTable">','<table style="border-collapse: collapse;" class="wrapped confluenceTable" border="1">').replaceAll("<p",'<p style="font-family: helvetica; font-size: 10pt;" ').replaceAll("<h1",'<h1 style="font-family: helvetica; font-size: 17pt;" ').replaceAll("<h2",'<h2 style="font-family: helvetica; font-size: 12pt;" ').replaceAll("<h3",'<h3 style="font-family: helvetica; font-size: 9pt;" ').replaceAll("<h4",'<h4 style="font-family: helvetica; font-size: 8pt;" ').replaceAll("<h5",'<h5 style="font-family: helvetica; font-size: 7pt;" ').replaceAll("<h6",'<h6 style="font-family: helvetica; font-size: 5pt;" ').replaceAll("<pre",'<pre style="font-family: monospace; font-size: 10pt;" ').replaceAll('<td','<td style="font-family: helvetica; font-size: 10pt; ').replaceAll('<th','<th style="font-family: helvetica; font-size: 14pt; background-color: #e6e6e6; text-align: center;" '); //replacing headers and tables to company standard
gr.author = k.history.createdBy.email;
var sysid = gr.insert();
var s = new sn_ws.RESTMessageV2('Confluence','Attachment_list');
s.setStringParameter('confluenceID', confluenceID);
var response3 = s.execute();
var responsebody3 = response3.getBody();
var httpStatus3 = response3.getStatusCode();
var l = JSON.parse(responsebody3);
//var att=[];
for(var c =0; c<l.results.length;c++){
try{
var z = new sn_ws.RESTMessageV2('Confluence','Attachment_download');
z.setStringParameter('confluenceID', confluenceID);
z.setStringParameter('attachmentID',l.results[c].id);
z.saveResponseBodyAsAttachment('kb_knowledge',sysid,confluenceID+"/"+l.results[c].title.replaceAll(" ","%20"));
var response4 = z.execute();
var responsebody4 = response4.getBody();
var httpStatus4 = response4.getStatusCode();
}
catch(ex){
var errormsg = ex.getMessage();
}
//var obj = {id:l.results[c].id,title:l.results[c].title};
//att.push(obj);
}
var kb = new GlideRecord('kb_knowledge');
kb.get(sysid);
kb.query();
gs.log("body " + kb.text);
while(kb.next()){
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', kb.sys_id);
att.query();
while(att.next()){
var pl = 'src="https://yourcompanyname.atlassian.net/wiki/download/attachments/'+att.file_name+'?api=v2"';
kb.text=kb.text.replaceAll(pl,'src="/sys_attachment.do?sys_id='+att.sys_id+'"/'); //replacing link to attachment in text file
kb.update();
}
}
var x = new sn_ws.RESTMessageV2('Confluence', 'POST_Label');
x.setStringParameter('confluenceID', confluenceID);
var response6 = x.execute();
var responsebody6 = response6.getBody();
var httpStatus6 = response6.getStatusCode();
}
If you have any questions feel free to contact with me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2022 02:49 AM
Yah! Super awesome work. I will play around with it and provide any feedback and any issues I run into.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2022 03:08 AM
good spot, thx Dorian! 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 01:03 AM
Hi Dorian,
Did this work as expected? Did you run into any problems?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 06:41 AM