Making a Dynamic Service Portal File Download Widget

jmiskey
Kilo Sage

We currently have a few Catalog Items on our Service Portal where users need to download a file that we have uploaded to ServiceNow.  So, we have create custom Service Portal Widgets.  They are pretty basic, here is all there is to them:

Body HTML template section:

<div>

<a href="/sys_attachment.do?sys_id=...">Link text to display</a>

</div>

Client Controller section:

function ($scope,$location) {
  $scope.download = function(path){
  $location.path(path);
  };
}

So, the sys_id and "text to display" are hard-coded right inside our widget.  And then in our Catalog Item, we use a "Label with Macro" variable, filling out the Widget box with the name of our widget.

This works great, but we currently have a separate widget for each file.  I have a whole bunch more files to add for a bunch more Catalog Items that I need to create, and instead of creating a bunch more widgets, I would like to create one dynamic file download widget, where I can just feed in the sys_id of the attached file and the text I would like to display.  I am just unsure how to update the widget, and how to feed in the values (attributes, maybe?).

I imagine this is probably a pretty easy request for an experienced widget creator, but I am pretty new to the widget world, and just getting my feet wet here.

Thanks

 

 

1 ACCEPTED SOLUTION

Joe & Will,

Yes, exactly... you're embedding the widget inside the catalog item widget using the Macro field. It will work perfectly with one or a dozen attachments.

From the inner widget, simply call $sp.getParameter('sys_id) to get the ID of the catalog item. From there you can use GlideRecord to get the URL and Label of the attachment, construct an Array on "data" and then loop through it in HTML using ng-repeat.

Hope that helps!

View solution in original post

7 REPLIES 7

jmiskey
Kilo Sage

Thanks Nathan!

jackiemcnabb
Giga Contributor

would ya'll be willing to share this widget?

 

Also how did you upload the files and reference them in the widget?

 

Thanks

vartika
Tera Contributor

Hi @nathanfirth ,

I  am doing the same, in my catalog item i have uploaded the attachment. and the sys_id i got from the attachment table i pasted in the widget. 

<div class="form-link" id="formlink1" style="padding-top:10px;">
<span><label class="fontcolor-{{get_theme}}">Click here for</label><a href="/sys_attachment.do?sys_id=4aeb3394db9c6b00c345fbfcbf96191a" 
style="color: red;" > USB device SOP document</a></span>




i am getting below after clicking on "Click here for USB device SOP document"

vartika_0-1735646490228.png



What should we check here? We are giving everything correct. Please Advice

Thanks in Advance!