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?

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.

6
  • 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 4:24
  • I have used : SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
    – variable
    Mar 24 '14 at 4:25
  • and there is no SP.js reference.
    – variable
    Mar 24 '14 at 4: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. Mar 24 '14 at 8: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 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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