How do I get the selected template from the print dijit?

4309
9
Jump to solution
02-12-2015 02:17 PM
RyanClancy
Occasional Contributor

I'm getting a list of templates from my Export Web Map task and putting them in the print dijit like in this sample. I want to record metrics about the usage of our print service and I want this to include which template the user selected for printing the map. However, it looks like the selected template is not a property of the print dijit. When I do something like this:

print.on("print-start", function(evt){
     console.dir(evt);
});

I do not see anything in the print dijit that gives me what I need. I can see the entire list of all available templates, but no way to tell which one the user selected. Anybody have any ideas?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

Ryan,

You don't need to extend the print widget. Just add the setReqeustPreCallback to your app. Here's how you can test this.

1. View the print widget sample in the sandbox.

2. Paste the code below right after the line that sets the proxy then run the app and you should see an alert box with the selected print layout name each time you make a selection.

 

        esriRequest.setRequestPreCallback(function(ioArgs){
          if(ioArgs.url === "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2..."){
            alert(ioArgs.content.Layout_Template);
          }
          return ioArgs;
        });

View solution in original post

0 Kudos
9 Replies
ChrisSergent
Regular Contributor III

Have you tried getting template.layout? The reference is here: PrintTemplate | API Reference | ArcGIS API for JavaScript

0 Kudos
RyanClancy
Occasional Contributor

Hi Chris,

That's the problem, there's no way to get the user selected template from the dijit. The print dijit has a templates property which contains an array of all available templates, and within that array I can access the layout of each template as you suggest, and in fact I am doing this in order to set title, subtitle, author, etc, on each template prior to printing. What I'm trying to do is detect which template the user selected for printing. Here's what's happening:

- user chooses a template

- the print dijit does a POST to the ExportWebMap task on my server

- the post contains a WebMapAsJSON object which does contain the name of the selected template (I can see this in Firebug but cannot access it programmatically via JavaScript)

- the server creates the output map and returns the url so the user can get the resulting map

.....so, it seems like the only place the selected template is available is in the POST to the server, but this isn't helpful because we cannot access POST data via JavaScript.

I've tried accessing the evt object for the 'print-start' and 'print-complete' events but it only includes the target (the print dijit) and the result object (url to the printed map).

I think I'm out of luck.

0 Kudos
ChrisSergent
Regular Contributor III

Just another possibility you can try; I have an sample page for printing that does not print as soon as the user selects a template. Perhaps you could get the template based on onClick event of the print button in my sample: Print a Map

Select the hammer button.

Click on the printer button.

It will then bring up the print dialog. The button is btnPrintReady, I would try to get the selected value of printLayoutId using the onClick event of the btnPrintReady 

Here is an example on how to get a value from an option list:

drop down menu - Get selected value of dropdownlist using JavaScript? - Stack Overflow

RyanClancy
Occasional Contributor

That looks promising. I'll try it out, thanks!

0 Kudos
KellyHutchins
Esri Frequent Contributor

If I understand the question correctly you can use esriRequest.setRequestPreCallback function to get the info. setRequestPreCallback lets you define a function that is called before esri.request and allows you to see the info that is associated with the request.

Here's a simple example showing how it works. You inspect the args to look for the print task execute url and when found you can then access the content associated with that request which includes the selected LayoutTemplate.

        esriRequest.setRequestPreCallback(function(ioArgs){

          if(ioArgs.url === "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2..."){

            console.log(ioArgs.content.Layout_Template);

          }

          return ioArgs;

        });

RyanClancy
Occasional Contributor

Ah ha! Thanks Kelly, that looks like it will do the trick. I'll try it out when I'm back at work on Monday.

Ryan

0 Kudos
RyanClancy
Occasional Contributor

Kelly,

Is there a way to attach the esriRequest.setRequestPreCallback function to an instance of esri/dijit/Print? My assumption is that the print dijit creates an esriRequest using the url property in the constructor, and this is how the print job is sent to the server.

Could I extend esri/dijit/Print to allow this somehow? Whatever mechanism is utilized in the dijit for communicating with the server does not appear to be exposed. When I inspect my instance of the print dijit in Firebug I don't see any properties that would allow me to access the communication between the dijit and the server.

Thanks,

Ryan

0 Kudos
KellyHutchins
Esri Frequent Contributor

Ryan,

You don't need to extend the print widget. Just add the setReqeustPreCallback to your app. Here's how you can test this.

1. View the print widget sample in the sandbox.

2. Paste the code below right after the line that sets the proxy then run the app and you should see an alert box with the selected print layout name each time you make a selection.

 

        esriRequest.setRequestPreCallback(function(ioArgs){
          if(ioArgs.url === "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2..."){
            alert(ioArgs.content.Layout_Template);
          }
          return ioArgs;
        });
0 Kudos
RyanClancy
Occasional Contributor

Awesome. I didn't realize that the .setRequestPreCallback() would apply to all instances of esriRequest in my app, which is why I was confused after your initial response.

Your solution works perfectly, thanks!

0 Kudos