Saturday, 14 July 2018

SP.UI.ModalDialog.showModalDialog() do not work in SharePoint 2013

After migrating 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.

function ShowServerInformation() {
var options = {
url: ‘/_layouts/Management/GeneralInformation.aspx’,
tite: ‘Management Information’,
allowMaximize: false,
showClose: true,
width: 430,
height: 230
};
SP.UI.ModalDialog.showModalDialog(options);
return false;
}

It’s very easy to fix this problem.

Follow below steps:
1. Remove the Java Script reference.
2. <script src=”/_layouts/sp.js” type=”text/javascript”></script>
3. <script src=”/_layouts/SP.UI.Dialog.js” type=”text/javascript”></script>
4. Add to the url variable “?IsDlg=1″`
5. Replace the command SP.UI.ModalDialog.showModalDialog() with the new command SP.SOD.execute(‘sp.ui.dialog.js’, ‘SP.UI.ModalDialog.showModalDialog’, options);

After this few changes your solution will work correctly.

SharePoint 2013 Example:
function ShowServerInformation(featureId) {
var options = {
url: ‘/_layouts/Management/GeneralInformation.aspx?IsDlg=1’,
tite: ‘Management Information’,
allowMaximize: false,
showClose: true,
width: 430,
height: 230
}
SP.SOD.execute(‘sp.ui.dialog.js’, ‘SP.UI.ModalDialog.showModalDialog’, options);
return false;
}

Note: 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.

No comments:

Post a Comment