2

I have the following code

(function ($) {

 $(document).ready(function () {
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    //SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'sp.userprofiles.js');
    SP.SOD.executeFunc('sp.userprofiles.js', 'SP.ClientContext', loadUserData);
});



var userProfileProperty;

function loadUserData() {

    //Get Current Context   
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Property to fetch from the User Profile
    var propertyName = "PreferredName"; 

    //Domain\Username of the user (If you are on SharePoint Online) 
    var targetUser = "i:0#.f|membership|[email protected]";

    //Lets make it dynamic
    //this.oWeb = clientContext.get_web();
    //var targetUser = this.oWeb.get_currentUser();


    //Create new instance of UserProfileProperty
    userProfileProperty = peopleManager.getUserProfilePropertyFor(targetUser, propertyName)

    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {

    document.getElementById("currentUser").innerHTML = userProfileProperty.get_value();
    var messageText = "\"Preferred Name\" property is " + userProfileProperty.get_value();

    //alert(messageText);


}

function onFail(sender, args) {
    alert("Error: " + args.get_message());
}

})(jQuery);

For some reason this code only fires when I edit the page and not when the page loads which is the exact opposite of what i need?!

5
  • 1
    If you view source, is SP.UserProfiles.js being loaded on the page? Commented Feb 28, 2013 at 16:53
  • Yes if I view source i can see the link to the JS and click on it which shows the the full JS file content.
    – Stephen
    Commented Feb 28, 2013 at 18:09
  • Are you seeing any JS errors in the console? Commented Feb 28, 2013 at 18:17
  • No errors after I changed SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'sp.userprofiles.js'); to SP.SOD.executeFunc('sp.userprofiles.js', 'SP.ClientContext', loadUserData);
    – Stephen
    Commented Mar 4, 2013 at 12:49
  • Did you get solution? I have similar problem. SP.js is getting loaded but Sp.UserProfile.js is not getting loaded.
    – user18770
    Commented Aug 7, 2013 at 10:54

4 Answers 4

4

Prefer SP.SOD.executeFunc over SP.SOD.executeOrDelayUntilEventNotified since it supports on demand scripts.

The following example demonstrates how to ensure UserProfile JS Library (SP.UserProfiles.js) is loaded in SharePoint 2013:

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

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

      //...

   });

});
2

In SharePoint 2013 as a part of Minimal Download Strategy, there are few OOB js files that don't load as they used to be in SP 2010. These include files like SP.js , SP.Runtime.js etc. So, you need to explicitly add references to these files. Either you can directly add the reference of SP.Runtime.js and SP.js in your master page or you can use getScript to load these files.

$(document).ready(function(){
    var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js",
               function () {
            $.getScript(scriptbase + "SP.UserProfiles.js", YourMethod);
          });
       });

});

1
  1. Try to make script name lowercase. There is an issue in RegisterSodDep which sometimes prevents loading not-lowercased scripts.
  2. Try to use executeFunc which forces script load.

Example code for "executeFunc":

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeFunc("sp.userprofiles.js", null, loadUserData);
3
  • Thanks Andrey...i will give this a go tomorrow, my js file is called userProfile.js - so you suggest changing this to userprofile.js?
    – Stephen
    Commented Feb 28, 2013 at 18:11
  • No, I suggest passing string "sp.userprofiles.js" to the SP.SOD.executeOrDelayUntilScriptLoaded instead of "SP.UserProfiles.js". Commented Mar 2, 2013 at 0:25
  • Ok tried using the lower case string and no difference...
    – Stephen
    Commented Mar 4, 2013 at 12:25
-3

Use capital "E" in "ExecuteOrDelayUntilScriptLoaded".

1
  • doing that will break the code.
    – Stephen
    Commented Aug 6, 2013 at 10:42

Your Answer

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

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