1

When I call the code below:

script type="text/javascript" src="/_layouts/15/SP.js" /script

script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js" /script

script type="text/javascript" 

var personProperties;
  function getUserProperties() {

            // Get the current client context and PeopleManager instance.
            var clientContext = new SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

            // Get user properties for the target user.
            // To get the PersonProperties object for the current user, use the
            // getMyProperties method.
            personProperties = peopleManager.getMyProperties();

            // Load the PersonProperties object and send the request.
            clientContext.load(personProperties);
            clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
        }

        // This function runs if the executeQueryAsync call succeeds.
        function onRequestSuccess() {

            // Get a property directly from the PersonProperties object.
            var messageText = " \"DisplayName\" property is "
                + personProperties.get_displayName();

            // Get a property from the UserProfileProperties property.
            messageText += "<br />\"Department\" property is "
                + personProperties.get_userProfileProperties()['Department'];
            alert(messageText);
        }

        // This function runs if the executeQueryAsync call fails.
        function onRequestFail(sender, args) {
            alert("Error: " + args.get_message());
        }

        (function () {
            //REST API
$(function () {
            SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
});
})(); 

The getMyProperties() function rises an error like: undefined is not a function; can someone help me? It seem's to be internal on SP.UserProfile.js

2 Answers 2

1

-How are you creating clientContext? Is this a SharePoint Hosted app? If so, you need to use the SP.RequestExecutor.js library

-You need a reference to SP.UserProfiles.js for this code to work (in case you have not already added it)

-Ensure that SP.UserProfile.js is loaded before you run your code.

  $(document).ready(function(){         
      SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');   
  });

Full thread about a similar problem here

3
  • 2
    RequestExecutor is for cross domain issues; you do not need to query the host for user profile properties, the app just needs the appropriate permissions. sharepoint.stackexchange.com/a/61724/20615
    – wjervis
    Commented Mar 2, 2015 at 20:37
  • 1
    And the first line of code shows how the ClientContext is created...
    – wjervis
    Commented Mar 2, 2015 at 20:38
  • As a magic the code start run with no erros and no modification. Commented Mar 3, 2015 at 13:35
0

I had the same problem. In my case I had to load runtime.js before userprofile.js is loaded:

SP.SOD.registerSod('sp.runtime.js', '\u002f_layouts\u002f15\u002fsp.runtime.js');
SP.SOD.executeFunc('sp.runtime.js', 'SP.ClientObject', function () {
    SP.SOD.registerSod('sp.runtime.js', '\u002f_layouts\u002f15\u002fsp.runtime.js');
    SP.SOD.executeFunc('sp.runtime.js', 'SP.ClientObject', function () {
        SP.SOD.registerSod('sp.UserProfiles.js', '\u002f_layouts\u002f15\u002fsp.UserProfiles.js');
        SP.SOD.executeFunc('sp.UserProfiles.js', 'SP.UserProfiles', function () {

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

            var profilePropertyNames = ["PreferredName", "Department"];
            var userProfilePropertiesForUser = 
                new SP.UserProfiles.UserProfilePropertiesForUser(
                    clientContext,
                    _spPageContextInfo.userLoginName,
                    profilePropertyNames);

            var userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);

            clientContext.load(userProfilePropertiesForUser);
            clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);

            function onRequestSuccess() { 
                alert(userProfileProperties[0]);
            }

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

        });
    });
});

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.