Tuesday, April 17, 2007

TaskPanes and Javascript in InfoPath 2003

Using HTML, CSS and JavaScript in Task panes is sometimes very tricky. Some functionalities are not allowed, but InfoPath do not alert errors … frustrating … isn’t it ?

HTML/CSS samples

  • Show/Hide DIV tags

Javascript samples

  • onload event of BODY tag
  • javascript included in href attribute of anchor
  • you are not allowed to talk to window.external.Window.XDocument; when using ASP.NET pages in a fully trusted, signed form.

Printing the Task pane’s content

Do not add the javascript:window.print() method not to the href attribute of the anchor tag to print … In this way (using href) you are not sure (100%) that the print functionality works! InfoPath 2003 do not generate errors when you try to trigger the print link, but … in Internet Explorer this works perfectly!

A possible solution is using the onClick event of the anchor tag. In this way you are sure that the window.print() method is called and the Print window will appear.

There is no other way to print the content of a custom task pane. So use the window.print();

Export to Word and Excel

It is quit simple to export your custom task pane content to Word and Excel. Because Word and Excel supports HTML, we can save the content of our custom task pane (html) as Word or Excel document

Suppose you an ASP.NET web page (.aspx) which button to export content to Word and Excel.

When the user clicks on the ‘export to Word’ button, we will dynamically add a very small IFrame tag to the page which will call another ASP.NET page. This page is responsible to generate the word or excel document. You can use the QueryString to add a parameter which indicates the type of export (xls/doc).

Response.AddHeader("content-disposition", "attachment;filename=defaultExportFileName");
Response.ContentType = @"application/msword";
Response.Write(“your html content”);

The Content Types are defined in the register (HKEY_CLASSES_ROOT). Select the .doc or .xls folder and have a look at the Content Type.

When clicking on the ‘Export to Word’ button, a download window will pop up to open or save your document.

Use HTML pages in fully trusted, signed forms

It is impossible to call window.external.Window.XDocument when using an ASP.NET page in a fully trusted, signed form. The external window seems to be unavailable at that moment.

If we use an static HTML page as Task pane, we can access the external window!
I suggest to communicate from InfoPath 2003 to the task pane. The communication in the other way is quit difficult if you use fully trusted, signed forms.

Using parameters in HTML Task Pane

It is possible to use a QueryString, also with a static HTML page which is included as resource file. Just call aHtmlTaskpaneObj.Navigate("start.html?key=value");

You can parse the QueryString by using JavaScript. [Read article about parsing javascript]

Hiding Task panes at startup

It is not allowed to hide Task Panes in the Start Up method because not all InfoPath objects are loaded at that time. So it is possible that your Task pane is not hidden, when the form is started. The time the task pane needs to hide depends on the form load.

No comments: