How to retrieve an attachment from service now via SOAP Web Service

sherman_1206
Tera Contributor

So after trial and error and some help from service now we are now able to successfully retrieve attachments from service now via SOAP Web Service. The basic idea is the following: attachments are stored in sys_attachment_doc table in 4k chunks, the attachment information is stored in sys_attachment table. The sys_attachment table stores file's meta information including, file name, file size, file type.

The sys_attachment_doc table stores files in the following way. When service now receives an attachment, they take it gzip it, split the gzip into 4k chunks and then do a base64 encoding of the 4k chunks. So to retrieve is as simple as finding the 4k chunks, base64 decoding, constructing the gzip archive and then extracting the file from the archive.

The trick is finding the attachment chunks. This is simple for example to find an attachment of an incident, we can look in sys_attachment table for table = "incident", table_sys_id = "". This type of query would return an attachment record, we can take the sys_id field of this record and then search sys_attachment_doc table for all records containing sys_attachment = "". This will return 1+ records and each of these records contains a field "position". Position defines ordering, and simply is 0,1,2,3,.. for reconstructing. So during your query in sys_attachment_doc table you can add "__order_by"=>"position" to retrieve the records in order.

And as mentioned before once we have the chunks, we base64_decode each chunk, then build a gzip archive and then inflate the archive and voila you have your attachment.

-Cheers

(Thanks to service now, and the wiki for finding a solution to this problem.)

Currently we have an enhancement request for an easier way to make one soap call and retrieve a file. I will update if this is implemented.

27 REPLIES 27

prasadhsp
Kilo Explorer

Hello Experts,

I am working no a requirement to fetch attachments from an external run book automation system. However I don't know how to get the attachment from the 4k chunks of data stored in sys_attachment_doc table. Can some one kindly guide on steps to get these from the chunks or any easier way ?

From the browser we can get attachments by doing this : https://instancename.service-now.com/sys_attachment.do?sys_id=whatever. However i am not able to simulate this browser download of attachments. Even tried wget but it returns a html page rather than an attachment.

Any pointers will be really helpful!!

Thanks in advance for your time.


I did find a mechanism to do this via generating a zip of attachments on the servicenowguru.   http://www.servicenowguru.com/scripting/download-attachments-zip-file/



You should be able to modify this slightly to retrieve the byte stream directly.  



This will require creating a processor and interfacing the processor instead of sys_attachment.do  


manojpotluri
Kilo Explorer

Chris,


do you have an update on this ?


"Currently we have an enhancement request for an easier way to make one soap call and retrieve a file. I will update if this is implemented."




Thanks,


Manoj Potluri.


I have not seen this enhancement be pushed into a servicenow release yet.  



The attachment is stored in the attachment table as a base64 encoding of the file.  



Easiest way I have found is to retrieve all the sys_attachment records.


Concatenate the values and decode the base64.


Write this base64 out to a file.  



If anyone has a better solution I am all ears.  


Chris,


Thanks for the reply, i am using your technique to retrieve the attachment using soap web services, one thing i am un-clear on is about compression, when i write a concatenated base64 string to a file what is the file type ?



Thanks,


Manoj Potluri.