7

I am using the javascript object model on the master page to fetch user profile properties.

Do I need to use the foll. script tags for the code to work or are they optional?

    <SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
        ondemand="false" localizable="false" loadafterui="true" />
    <SharePoint:ScriptLink ID="ScriptLink2" name="<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />

<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />" runat="server"
        ondemand="false" localizable="false" loadafterui="true" />

My code works when I remove them, it also works with them but sometimes it gives 'runtime error'. Just want to know if it is safe to remove them. This msdn link specifies we have to use them on the application page. Can we do away with it on master page?

variable
  • 4,473
  • 13
  • 75
  • 139

1 Answers1

15

Since SP.SOD.executeFunc supports on demand scripts, there is no need to reference SP JavaScript files using SharePoint:ScriptLink in master pages.

The following example demonstrates how to initialize UserProfiles CSOM in application page (default master page is used):

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   // Make sure PeopleManager is available 
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {

      var clientContext = new SP.ClientContext.get_current();
      var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

      //...

   });

});

SP.SOD.executeFunc vs SP.SOD.executeOrDelayUntilScriptLoaded

Since SP.SOD.executeFunc supports on demand scripts, but SP.SOD.executeOrDelayUntilScriptLoaded does not, the following code:

   SP.SOD.executeOrDelayUntilScriptLoaded(function() {

      var clientContext = new SP.ClientContext.get_current();
      var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

      //...

   }, 'SP.UserProfiles.js');

will not be executed if SP.UserProfiles.js has not been loaded.

Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
  • OK, so your answer is that: On the master page or any other page (application page) or webpart/user control, there is no need to reference the SP.js and SP.UserProfiles.js only if we use SP.SOD.executeFunc ? Also, what about my code, I have not used Sp.SOD so do I need to reference them? – variable Mar 24 '14 at 04:24
  • I have used : SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js'); – variable Mar 24 '14 at 04:25
  • and there is no SP.js reference. – variable Mar 24 '14 at 04:26
  • Please check the updated answer it is recommended to use SP.SOD.executeFunc instead of SP.SOD.executeOrDelayUntilScriptLoaded in application pages. Regarding SP.SOD namespace, it is a part of common library (init.js) which is loaded with every page in SP. – Vadim Gremyachev Mar 24 '14 at 08:44
  • OK, thanks, so if we use executeordelay then we have to explicitly specify the script links?. But, what about master page, should I call the script links and use executeasync or executeordelay? – variable Mar 25 '14 at 04:29
  • Also, you say I should not use scriptlink then what do I use script tag? – variable Mar 25 '14 at 04:55