Hacking SP.UI.ModalDialog to download ReportServer PDF

We have a situation where we want to let our users click a report server link, but the report can take (depending on parameters) anywhere between 5 to 30 seconds to create and export as PDF.

Here's how you can "hack" some JavaScript to make the experience better.

 

Use SP.UI.ModalDialog

function do_modal(url, title, callback) {
    var options = {
        url: url,
        title: title,
        showClose: true
    }
    if (callback) {
        options.dialogReturnValueCallback = Function.createDelegate(null, callback);
    }
    return SP.UI.ModalDialog.showModalDialog(options);
}

 

This lets me call the Report Server URL via:

var d = do_modal(url, title);

I'm catching the dialog object, because I want to do a few more things.

 

Pesky IsDlg=1

Report Server is very perculiar with the Query paramaters passed in, and SP Modal Dialog likes to add a IsDlg=1 to the query string, this doesn't play well. 

image

"An attempt was made to set a report parameter 'IsDlg' that is not defined in this report.  (rsUnknownReportParameter)"

You can fix this two ways, first, if you have access to report server, you can just add a unused optional parameter IsDlg. 

If you don't have access to report server, you now need to hack the ModalDialog, which is what I did next.

 

Give me an IFrame, and I will give you a SRC

var f = d.get_frameElement();
f.src = url; // remove IsDlg=1

 

I ask for the IFrame object created by the dialog, and then brute force the SRC attribute to my url.  This undo the extra IsDlg appended by the ModalDialog.

 

Awesome, my report is now being downloaded, while the user waits with a nice friendly modal popup :-)

image

 

But wait, hmm, big problem...  screen is stuck

:-(

image

Because the PDF file is downloaded, the browser never receives the signal to stop and dispose the Modal Dialog.  Now the site is stuck.  Which brings us to my final trick.

 

Manually attach and watch the IFrame, and dispose the dialog when we're done

var t = function() {
    // ready state goes from "loading" to "interactive"
    if (f.readyState != 'loading') {
        d.close(SP.UI.DialogResult.cancel);
        return;       
    }
    setTimeout(t, 2000);       
};

setTimeout(t, 2000);

 

Let's define a function t, we'll be watching the readyState property of the IFrame object.  When it is no longer loading, we'll close the modal dialog.  Wait and recalculate every 2 seconds until this happens.

Here's the MSDN article on IFrame.readyState

http://msdn.microsoft.com/en-us/library/ms534359(v=VS.85).aspx

One of the more interesting thing is that when you are downloading a PDF, the readyState remains in the "interactive" state, and never reaches the "loaded" state.  This is the reason a loaded() event doesn't fire, and thus the default SharePoint ModalDialog doesn't know we're done and need to fix itself.

 

Everything in one go:

 

    var d = do_modal(url, title);
    var f = d.get_frameElement();
    f.src = url; // remove IsDlg=1
    var t = function() {
        // ready state goes from "loading" to "interactive"
        if (f.readyState != 'loading') {
            d.close(SP.UI.DialogResult.cancel);
            return;       
        }
        setTimeout(t, 2000);       
    };    
    setTimeout(t, 2000);

Not many lines of javascript, with an awesome result.

InfoPath - an example of using an XML file for special characters

This is an old trick relating to inserting special characters (such as carriage returns) and tricking InfoPath to stop removing them! 

http://blogs.msdn.com/b/infopath/archive/2005/03/04/385577.aspx

I've decided to take some screenshots to show what you should be expecting when you apply this technique.

 

The XML file

image

 

The Data Connection

image

image

 

A formula to use it

image

The advanced Xpath is:

concat("my", xdXDocument:GetDOM("characters")/lookups/characters/@crlf, "hello", xdXDocument:GetDOM("characters")/lookups/characters/@crlf, "world")

 

Result - Rich Client

image

 

Result - Web Browser Form

image

 

The HTML generated in the browser form is:

<TEXTAREA ...>my
hello
world</TEXTAREA>

 

Notice it has the correct carriage returns creating the new lines.

 

The example XSN file if you want to download it and have some fun

https://static1.squarespace.com/static/5527bff2e4b0b430660b0d10/5527c30de4b030eeeef09715/5527c30fe4b030eeeef0a0ca/1318394572343/crlf.xsn

SPSCBR - REST Service and jQuery AJAX

Climbed out of bed at 4am and drove myself and colleague Jumpei on a laughter-filled trip down to Canberra, where we had lots of fun at the SharePoint Saturday Canberra event.

Our best joke along the drive was debating whether Lake George is a real lake.  Our most scary moment was when the car wouldn't start back up after we took a 10min powernap.

 

As promised, my slide deck and a zip file of the VS.NET solution that was shown. 

While the concepts are still fresh you should build a rest service and see how this all works for you.  Enjoy :-)

Special thanks to Ishai Sagi who saved the day with an innovative 2-laptop remote-each other and project via SVG port.

 

Ran out of time, I couldn't cover in more detail...

  • How to pass multiple parameters to REST, via a POST operation - the example is in my script and Rest service, but commented out follow that pattern.
  • jQuery AJAX calls are asynchronous, you can fire 4 requests at the same time and they will run in parallel.  Number of asynchronous calls depends on browser but 4 is a common-minimum.
  • Debugging javascript tips
  • Packaging CSS/JS files in a sandbox solution tips
  • Some consideration and discussions around sandbox solution webpart and quota

 

Files

Related Links

For 2007, unfortunately you can't easily do REST, but a lot of the SOAP service wrappers are done by awesome people so you don't have to do it yourself.  Thank Marc.

InfoPath - abusing a secondary datasource as temporary variable

 

This is an InfoPath technique that I've recently started using, and is pretty awesome.

Problem:

  • InfoPath is great with rules and fields, but modifying the field means that your form has now changed.  In code behind we have formstate variables, but they are not visible by InfoPath rules.
  • We may want to change these fields in later versions of the template.
  • If only we can have temporary variables to use in InfoPath

Solution in one sentence:

  • Create a secondary datasource, use abuse it to hold variables that you don't want to save to your main form

 

Real Example

How to detect if your form has been changed by the user since it was opened.  This is a tweaked version of Alec Pojidaev's How to check if your InfoPath form is “dirty”? (Tracking changes) using secondary temporary variables.

 

Steps

  1. Create a separate secondary datasource
    image
  2. Add the XML file as a secondary datasource
    image
  3. Now we have a secondary data source that we can use
    image
  4. Use that datasource to keep all the "UI" logic.  Such as:
    • "a checkbox to confirm whether I want to save and close, or just close"
    • A rich text field to show a message that I display on a separate message View
    • Various validation or toggle Booleans to control the UI
  5. Because the secondary datasource is not saved back to the main datasource, changes to the secondary datasource tree can be considered "discarded", effectively giving us "temporary variables" that rules can use.
  6. Create the form rules
    image

  7. Set the field "Form" on the secondary data source to that of the current main datasource
    image
  8. To test if the current form, at a later point in time, is the same as the form when it was opened
    image

Demo

  1. On open, is the form the same as when we opened?
    image
  2. After some changes
    image
  3. Delete our changes - "true" again
    image

 

How to use it

  1. When a form is opened, use rules (or code) to set the default values in the secondary datasource
  2. Through the lifetime of the form you can set the values to whatever you want, and use it in conditions
  3. When the form closes it doesn't get saved

Benefits

  1. You can track UI logic without actually modifying the main form
  2. You can then use the string(.) trick to save the current form on form-load, then use that value to compare with the form at a later date to see if the form had been changed by the user
  3. Some values, like "current user displayname" can be stored temporarily and is perfect for this
  4. If you didn't like a temporary variable, just remove them or rename them.  Because they are not saved to the main data source, you don't have to worry about breaking existing or future forms.

WindowsPhone 7.5 Mango and Office 365

 

Adding an account

  1. Go to settings | email + accounts | add an account | Outlook
  2. Provide Office 365 login email and password
  3. Once setup, the account will default to the name Outlook (or Outlook #)
  4. In settings | email + accounts, tab the Outlook entry once and you’ll be able to change the name to something more meaningful, like Office 365

Office hub

  1. Now in the Office hub, you can connect to your Office 365 account. 
  2. The first time you login it will open a web browser control and ask you to login.  You can choose to remember login and password to skip this step in the future.
  3. Once authenticated, you’ll see the a view of the lists and document libraries from your Office 365 team site

For those of us keeping itchy to write our own SharePoint - WindowsPhone applications, Microsoft cheated here and the web browser control used in step 2 is accessed via COM to obtain the cookiejar file, which contains the tokens for the Office hub to talk to Office 365.