SharePoint 2010 error: error creating modal dialog?

For some reason, the SharePoint modal dialog is not working properly. The error I am getting is this:

  • In Firefox: SP.UI.$create_DialogOptions is not a function
  • In IE: Object doesn't support this property or method

Here is my code:

 var options = SP.UI.$create_DialogOptions(); options.width = 525; options.height = 300; options.url = '/_layouts/mywork/richtexteditor.aspx'; options.dialogReturnValueCallback = Function.createDelegate(null, function (result, value) { alert(result + value); }); SP.UI.ModalDialog.showModalDialog(options); 

Interestingly, when I check the SP.UI in Firebug, I do not see all the methods and properties.

NOTE. I use the standard Webpart (not visual) and not the application page.

+7
source share
4 answers

The problem is that the required SharePoint JavaScript library was not loaded. (SharePoint 2010 JS is a good bit of clutter, namespaces, etc. the issue is complicated by a new on-demand download).

The library that must be loaded to use the Modal Dialog SP2010 interface (including $create_DialogOptions and showModalDialog ) is "sp.js".

To provide sp.js downloads:

 ExecuteOrDelayUntilScriptLoaded(function () { // do modal dialog stuff in here (or in another function called from here, etc.) }, "sp.js") 

The callback function is called only after "sp.js" (including the material SP.UI.ModalDialog ) is loaded (and it can never be called if there is a loading error).

This could also be solved using <ScriptLink> to sp.js with OnDemand , but I can’t guarantee it: (I think there might have been problems with this approach, but I can’t remember why it wasn’t used in the project I just looked at.)

 <SharePoint:ScriptLink runat="server" Name="sp.js" OnDemand="true" Localizable="false" /> 

See SPSOD for more details / information.

Happy coding.

+16
source

For me, it worked like this: ExecuteOrDelayUntilScriptLoaded(function () {}, "sp.js")

and:

 <SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="sp.js" OnDemand="false" Localizable="false" LoadAfterUI="true"/> 
+2
source

It is established that the Adela and user166390 approaches do not work in older IE 7-8. It seems that the page has not been fully parsed and tried to be resized using an interactive iframe, and this is bad for these IEs. For my case - I need to automatically open a popup dialog on the application page - I fixed it with the following

 <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"> <SharePoint:SPPageManager ID="SPPageManager1" runat="server" /> <script type="text/javascript"> var ShowDialog = function () { var options = { url: '/_login/default.aspx, title: 'Title, Description, and Icon', width: 640, height: 400, dialogReturnValueCallback: function(dialogResult, returnValue) { window.location.replace(returnValue); } }; SP.UI.ModalDialog.showModalDialog(options); }; ExecuteOrDelayUntilScriptLoaded(ShowDialog, "sp.ui.dialog.js"); </script> </asp:Content> 

This little thing

 <SharePoint:SPPageManager ID="SPPageManager1" runat="server" /> 

registers all SP javascripts.

This approach was found on the MSDN forums .

+1
source

You can fix this problem by using a generic object for an option instead of the DialogOptions class. this means you should write this option:

 //Using a generic object. var options = { title: "My Dialog Title", width: 400, height: 600, url: "/_layouts/DialogPage.aspx" }; 

for more usage information, visit: http://msdn.microsoft.com/en-us/library/ff410058%28v=office.14%29.aspx and see an example.

0
source

All Articles