Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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 at 4:24
    
I have used : SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js'); –  variable Mar 24 at 4:25
    
and there is no SP.js reference. –  variable Mar 24 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. –  Vadim Gremyachev Mar 24 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 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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