Monday, September 16, 2013

SP.UI.ModalDialog.showModalDialog(options) in SharePoint 2013

Have you ever tried using SP.UI.ModalDialog.showModalDialog(options) in SharePoint 2013. I discovered some strange behavior…
After migrating my code from SharePoint 2010 to SharePoint 2013 the calls to showModalDialog failed with message that the method cannot be found (javascript). When checking it in IE Developer Tools this isn’t surprising at all. The required js-file isn’t loaded.
But why? I guess it must be the new SOD-Model (Script on Demand) that was introduced in SharePoint 2013.
So I looked for a function that would force to load the missing js. Here it is:
SP.SOD.executeFunc(‘sp.ui.dialog.js’, ‘SP.UI.ModalDialog.showModalDialog’, function () { ShowUploadDialog(); });
With that function you can force IE to load the missing js with the needed method. The last parameter ist a delegate where you can e.g. call a function that uses the function you just ensured to be loaded.
Simple as that…
Notice: I first tried the “executeOrDelayUntilScriptLoaded”-function. But it was not of much help. It just “swallowed” my function call but never executed it because the js-file I specified was never loaded :-(
Hope this helps anyone out there!

No comments: