Static links for Knowledge base article attachments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2023 03:56 PM
Hello,
I am trying to create links to KB article attachments from another system, but if the file is updated, the link changes, which causes too much work to keep updating the links in the external system.
Is it possible to enable attachment file versioning in ServerNow KB? And how to link to the latest version of the file. It does not have to be KB article attachments; if there is a way to do this via another application in the system (like Document Management) that will give me a static link, it would be great too.
Thank you in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2023 04:20 PM
Can you share an example of the link you are using?
Something is telling me you are not using the "permalink" which will give you the latest version every time.
It should look something like this: <instance_name>/kb?id=kb_article_view&sysparm_article=<KB_number>
If I helped you with your case, please click the Thumb Icon and mark as Correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 06:31 AM - edited 01-10-2023 06:39 AM
Hey Leon, unfortunately all links to attachments are in this format: /sys_attachment.do?sys_id=ATTACHMENT-SYS-ID. Each attachment has a system generated SysID so if the attachment is updated the SysID changes which is likely what you are experiencing.
To solve your issue of having a consistent Number that will refer to a changing attachment, Knowledge Management is probably the best solution with features like versioning where KB number changes the same and Perma-links to provide a consistent URL. In platform, Knowledge Management has an "attachment link" feature which is an "option for downloading an attached file automatically when a user accesses the article, instead of opening the article view", but this won't help you based on your requirement to have a consistent URL to the attachment from an external source. Fortunately there are platform features that can help you solve this use case via configuration.
Scripted REST APIs allow you to execute a script to perform an action. This will provide a consistent URL that your external system can include and then ServiceNow will run a script to perform an action. I propose that you create one that will accept a KB number and then it will query for the attachment linked to that article and then prompt the user to download it. The following steps should get you started.
- Choose what application scope you wish to include this functionality into. Given Knowledge Management is a "global" feature I would suggest using "Global" but this is completely up to you and something you will need to test to ensure it works across all knowledge bases. Set your application scope appropriately.
- These configurations will also be captured in an update set so set your update set appropriately based on your SDLC process.
- Navigate to System Web Services \ Scripted Web Services \ Scripted REST APIs and click New
- Give the API a name, in my example I chose Knowledge Attachment and set the API ID to "kb_attachment"
- Right click on the header/Click Additional Actions and choose Save to remain on this form
- By default this API will require the user to login in order to access the attachment
- Scroll to the Resources Related List and click New. Here we will add the script to get the attachment linked to a provided KB number and prompt the user to download.
- Give it a name like "getAttachment" and set the following values:
- As you can see in the Relative path, there is a variable "kbNumber" that will be required within the URL so that the script knows which attachment to present.
- Right click on the header/Click Additional Actions and choose Save to remain on this form. Now look at the Resource Path value. This will be the URL that your external system will utilize to get the attachment.
- In my example it will be https://INSTANCE-NAME.service-now.com/api/snc/kb_attachment/get/KB-NUMBER
- Change this URL to match what is provided within your form obviously changing Instance Name and KB Number to match your values. Also note that "snc" within the URL may be different within your instance too.
- Then in the script paste in the following:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { //Get KB Number from URL var kbNumber = request.pathParams.kbNumber; //Query for a KB Article based on the provided number var kbKnowledge = new GlideRecord("kb_knowledge"); kbKnowledge.addQuery("number", kbNumber); kbKnowledge.query(); if (kbKnowledge.next()) { //If the KB article is found, query for the attachment var attachmentRec = new GlideRecord("sys_attachment"); attachmentRec.addQuery("table_name", kbKnowledge.getTableName()); attachmentRec.addQuery("table_sys_id", kbKnowledge.getValue("sys_id")); attachmentRec.query(); if (attachmentRec.next()) { //Attachment found now set variables for user to download the file var attachmentID = attachmentRec.getValue("sys_id"); var contentType = attachmentRec.getValue("content_type"); var fileName = attachmentRec.getValue("file_name"); var headers = {}; headers['Content-Type'] = contentType; headers['Content-Disposition'] = 'attachment;filename=' + fileName; response.setHeaders(headers); response.setStatus(200); var sysAttachment = new GlideSysAttachment(); var writer = response.getStreamWriter(); var attachmentStream = sysAttachment.getContentStream(attachmentID); writer.writeStream(attachmentStream); } } return; })(request, response);
- The above script will take the supplied KB number and query for a match. If a match is found it will then query for a linked attachment. If the attachment is found, it will then utilize the GlideSysAttachment API to stream the attachment to the user for download.
- Click Submit to create the resource record
- Now test by substituting the values in the URL mentioned above. Again if the user if not logged in currently they will be prompted to login before the attachment is presented.
Hopefully this gets you going. Please post any questions!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 01:01 PM
Michael,
Thank you so much! This is perfect!
I got to work; I just have a couple of follow-up questions.
1. My SNOW instance uses Azure SSO as an identity source, and even if I am logged in to Service Now in the browser, it still brings up the combo login box and asks for Username and Password. Once I log in, all consequent "get" operations don't require a password. Is there a way to make it "honor" the SSO token that is already in the browser session?
2. The script downloads the file into the computer download folder. Is there a way to open it in the SNOW PDF viewer? or instead of downloading it, force the browser to open it in a tab automatically?
Again, thank you much!