7

I built a WebPart for SharePoint 2010, in this WebPart, when I click a button, it will popup a dialog which contains an image, I just call the sp.ui.modaldialog api direcyly.

ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.ModalDialog.showModalDialog(options)}, “sp.ui.dialog.js”)  

As SharePoint 2013 is released, I want to move this WebPart to SharePoint 2013, but I met a problem after the migration. When an user log in, the popup works well, but if current user is anonymous, it doesn't work.
I tried to debug it, and I found the variable "g_ExecuteOrWaitJobs" contains all the events to be executed as soon as js is loaded. My function is also in the waiting jobs queue, but the property "notified" is false, which explains why popup is not triggered.
So I checked the scripts in the page, the developer tool shows that all the "sp.*.js" are not registered to the page.

So I got the reason, as "sp.ui.dialog.js" is not registered, my function will never be notified. but in SharePoint 2010, "sp..js" are registered for anonymous user(and also in "SharePoint 2010" view in SharePoint 2013), why is it changed? If we see the page source, it will register the "sp..js", why is it not registered at last?

<script type="text/javascript">RegisterSod("sp.ui.dialog.js", "\u002f_layouts\u002f15\u002fsp.ui.dialog.debug.js?rev=2hnmqeYvKPxuEddYqrW\u00252F0A\u00253D\u00253D");

How to call the SP.UI.ModalDialog.showModalDialog function for anonymous user? Thanks in advance.

6
  • Did you find any answer for this? I am also looking for the same answer or any relevant documentation. Dec 14, 2012 at 15:12
  • 1
    @VardhamanDeshpande you can use the "SP.SOD.executeFunc(key, functionName, fn)" method. It will load the script if it's not loaded yet. Dec 16, 2012 at 5:48
  • @VardhamanDeshpande The way of registering sp.js(for example) is different from registering it directly. <script type="text/javascript">RegisterSod(file, url) will not load js immediately. It will be loaded only when it's required.(for more details, please check this article:ilovesharepoint.com/2010/08/…) So I just guess that when current user is anonymous, some common functions will not be required and executed, and these functions are related to "sp.*.js", so the js files will not be loaded. Dec 16, 2012 at 5:57
  • Well my issue is a little different, when working with publishing sites, if a page is not checked in, then the sp.*.js files are loaded. But as soon as the page is checked in and published, the files stop loading. Dec 16, 2012 at 15:06
  • @VardhamanDeshpande I also met this problem, and I found a question that describes this issue:social.msdn.microsoft.com/Forums/en-US/sharepointdevpreview/…, but the solution in the question is quite unclear. If you pay attention to the ribbon(Browse,Page) in your page, you will find that after the page is published, the ribbon is gone. Because the ribbon relys on "sp.*.js", so if the ribbon appears in the page, sp.js will be loaded. All in all, for some reasons, sp.js is not required(ribbon is just one way that will require sp.js) in your page. Dec 17, 2012 at 6:38

3 Answers 3

1

@Xiadon Mao, you could try the following on your master page.

<SharePoint:ScriptLink ID="ScriptLink1" Name="SP.js" runat="server" OnDemand="false" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink ID="ScriptLink2" Name="SP.UI.Dialog.js" runat="server" OnDemand="false" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink ID="ScriptLink3" Name="SP.Ribbon.js" runat="server" OnDemand="false" LoadAfterUI="true" Localizable="false"/>

That's how I pre-load everything to make sure all the scripts work -- especially when I use other browsers, where the javascript loading occurs differently. (e.g. Google Chrome). I have some more on improving the loading, particular with Google Chrome, in my blog.

2
  • Thanks for your answer, your information is useful. But in our situation, we are not allowed to modify our customers' master page. I've found that SP.SOD.executeFunc(key, functionName, fn) can solve this problem. Feb 23, 2013 at 11:10
  • Great one. You have saved my day buddy :)
    – suryakiran
    Nov 20, 2013 at 14:55
1

I used Xiaodan Mao's solution. Here is my implementation:

In my custom javascript file I have:

SP.SOD.executeOrDelayUntilScriptLoaded(onSpLoad, "sp.js");

function onSpLoad() {
    var clientContext = new SP.ClientContext.get_current();
    //Continue with ClientContext code here
}

jQuery(document).ready(function () {
    SP.SOD.executeFunc("sp.js", SP.ClientContext);
});

This seems to solve the issue for me. I thought Quinn's answer was working, but Quinn's solution gave me a number of javascript errors on the page (i.e. type is not defined)

1
  • I like Phil's idea because you can place it only where you really need it and apply it to different script files. I used it on one page where SP.UI.Dialog.js wasn't loaded and it did the trick. This issue generally causes so much confusion and Msft should really do a better job of explaining how it all works.
    – matt
    Feb 17, 2015 at 19:43
0

Just make sure that the logged user has access to view the master page. In some sites, the users can access the subsite but cannot see the site collection where the master page is located.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.