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



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


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

  • 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

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



| improve this answer | |

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.

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


| improve this answer | |
  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);
| 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".

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

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.