Exporting a word file to OpenText but cannot be opened from OpenText

smicloud
Tera Guru

The real use case is that I should be importing a word file from OpenText (OT) using REST and then add it as a new version for another file in OT using REST. There are two problems: 

 

1. If I am taking an attached word file from some task (e.g. incident) I can export the file so it ends up as a new version in OT. However, when I click it in OT it will not open but instead I am getting the dialog opened saying "Word found unreadable content in <filename>. Do you want to recover the content...." and if clicking "Yes" I am getting another dialog saying "Word experienced an error trying to open the file...". Check my first example for this. 

2. When trying to get the imported file from the response body and directly export it, without adding it as an attachment to a case first, I am getting similar error. See my second example for this. 

 

I am using a flow action with a script step and a rest message/method to do the export. Data streams or actions with rest step may have a bug which makes it impossible to even export the document at all (according to ServiceNow support). "May", still waiting for a final confirmation on this. 

 

For this example I am defining the most of the message form here: 

 

 

var attachment = new GlideRecord('sys_attachment');
attachment.query('sys_id', <sys_id>);
attachment.query();

var fileContent = "";

if (attachment.next()) {
var gsa = new GlideSysAttachment();
fileContent = gsa.getContent(attachment);

}

var fileName = 'file1.docx';
var r = new sn_ws.RESTMessageV2('x_mscs2_mss.LivelinkRESTApi', 'Add version  - NO CONTENT');
r.setRequestHeader('Content-Type', 'multipart/form-data;boundary=--WebKitFormBoundary7MA4YWxkTrZu0gW');

var boundary = '--WebKitFormBoundary7MA4YWxkTrZu0gW';
var body = '--' + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + fileName + '"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n' + fileContent + '\r\n' + '--' + boundary + '--';

r.setRequestBody(body);
r.setStringParameterNoEscape('id', <id>);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

 

When printing the body to the log I am getting content like this: 

 

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="file1.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

PK!ߤ�lZ [Content_Types].xml �(����n�0E�����Ub袪*�>�-R�{V��Ǽ��QU�
l"%3��3Vƃ�ښl �w%�=���^i7+���-d&�0�A�6�l4��L60#�Ò�S

---cutting---

)NN~�"word/webSettings.xmlPK-!�/��z;$word/fontTable.xmlPK-!|d�|Z&docProps/core.xmlPK-!�y��q� )docProps/app.xmlPK��+
----WebKitFormBoundary7MA4YWxkTrZu0gW--

 

Anyone that can see anything that is not correct here? Or any other ideas why there could be a problem with the file/format when exporting it? As said, the file do end up at OT but I just cannot open it. 

 

If I am trying to export the file without adding as an attachment first I am doing like this: 

 

 

var r = new sn_ws.RESTMessageV2('x_mscs2_mss.LivelinkRESTApi', 'Get Content');
        r.setStringParameterNoEscape('id', <id>);
        var fileName = "file1.docx";

        var response = r.execute();
        var responseBody = response.getBody();

        var fileContentStr = '';
        for (var i = 0; i < responseBody.length; i++) {
            fileContentStr += String.fromCharCode(responseBody.charCodeAt(i) & 0xff);
        }

        var r2 = new sn_ws.RESTMessageV2('x_mscs2_mss.LivelinkRESTApi', 'Add version  - NO CONTENT');
        r2.setRequestHeader('Content-Type', 'multipart/form-data;boundary=--WebKitFormBoundary7MA4YWxkTrZu0gW');

        var boundary = '--WebKitFormBoundary7MA4YWxkTrZu0gW';
        var body = '--' + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + fileName + '"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n' + fileContentStr + '\r\n' + '--' + boundary + '--';

        r2.setRequestBody(body);
        r2.setStringParameterNoEscape('id', <id>);
        gs.error(r2.getRequestBody());
        var response2 = r2.execute();
        var httpStatus = response2.getStatusCode();

        var httpStatus = response2.getStatusCode();
        gs.error("FILE CONTENT RESPONSE CODE GETTING FILE: " + httpStatus + " >> " + response2.getBody());

 

 

And I am getting the same result as for the first example. Anything incorrect here? 

 

And when printing the body for this example it looks like this: 

 

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="file1.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

PK!0ýýý[Content_Types].xml ý(ýýný0EýýýýýUb`QUýEýýýL<!Výýýýýýýý
*DýDýgýgýDýpýýeýý5)ý'=ýlTfýýýýkýýýýýHQZ)ý@`ýýýýpýq"Rýýýý=rýýýu`ýý[ý­ýs'ý/1>ýýýyf 

---cutting---

ý#word/webSettings.xmlPK-!Gý`~ýýý$word/fontTable.xmlPK-!mýuýýý'docProps/core.xmlPK-!ýBtý3ýý*word/numbering.xmlPK-!ýýýGý3docProps/app.xmlPKMý7
----WebKitFormBoundary7MA4YWxkTrZu0gW--

 

I don't know what more input I can provide. Anyone having any idea what can be wrong? Encoding? How correct if it could be this? 

0 REPLIES 0