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

Mx.
  • 3,323
  • 1
  • 20
  • 41

2 Answers2

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

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

        });
    });
});
Emaborsa
  • 1,189
  • 1
  • 11
  • 34