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 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|developer@cndev.onmicrosoft.com";

    //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?!

share|improve this question
1  
If you view source, is SP.UserProfiles.js being loaded on the page? –  John Chapman Feb 28 '13 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 Feb 28 '13 at 18:09
    
Are you seeing any JS errors in the console? –  John Chapman Feb 28 '13 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 Mar 4 '13 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 Aug 7 '13 at 10:54

4 Answers 4

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);
          });
       });

});

share|improve this answer

where are you calling the js?

have you tried:

window.onload=function($) 
{
   //call any function here :) when the page loads 
}

if you are calling the js file from within a .cs file than use and eventhandler (onload) or onPreRender or CreateChildControls ect ect to call the js file to run

share|improve this answer
    
I'm not using code behind...All of the above code is in a userProfile.js file which is referenced in the Masterpage. The userProfile.js has been uplaoded to the SP site via the SP UI. –  Stephen Feb 28 '13 at 18:10
  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);
share|improve this answer
    
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 Feb 28 '13 at 18:11
    
No, I suggest passing string "sp.userprofiles.js" to the SP.SOD.executeOrDelayUntilScriptLoaded instead of "SP.UserProfiles.js". –  Andrey Markeev Mar 2 '13 at 0:25
    
Ok tried using the lower case string and no difference... –  Stephen Mar 4 '13 at 12:25

Use capital "E" in "ExecuteOrDelayUntilScriptLoaded".

share|improve this answer
    
doing that will break the code. –  Stephen Aug 6 '13 at 10:42

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.