Question

I am trying to get current user profile information using the PeopleManager object ( SP.UserProfiles.js ) in a SharePoint hosted app. Code snippet is like below :

var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
var userProperties = peopleManager.getMyProperties();

But, the peopleManager.getMyProperties() call gives an error "undefined is not a function"

The SP.UserProfiles.js is loaded correctly.

Whole Code:

        var clientContext = new SP.ClientContext.get_current();

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

        // error occurs at this line
        var userProfileProperties = peopleManager.getMyProperties();            

        clientContext.load(userProfileProperties);
        clientContext.executeQueryAsync(
            function() {
                // do nothing   
            }, 
            function(){
                // do nothing
            }
        );

The error occurs even before calling

                      clientContext.load(userProfileProperties);

So, it looks like the issue might not be while loading the object by client context.

How can I resolve this issue?

Was it helpful?

Solution

Besides that, you also need reference to SP.UserProfiles.js for this code to work (in case you have not already added it)

Check this link for a very good article on this subject .

Update: Working code

    var context = SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(context);
    userProfileProperties = peopleManager.getMyProperties();
    context.load(userProfileProperties);
    context.executeQueryAsync(success, fail);

OTHER TIPS

The problem is that because you're in a SharePoint Hosted app, you're in a separate domain than SharePoint. Since the user profiles belong to the SharePoint host domain, and not the app domain, getting user profiles from the current context will return nothing. In order to get them, you need the cross-domain library: SP.RequestExecutor.js.

Now, I've not used the cross-domain library with the user profiles, so I'm not 100% sure this will work, but try changing your code to the following:

var context = new SP.ClientContext(appweburl);
var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);

var peopleManager = new SP.UserProfiles.PeopleManager(appContextSite);
var userProfileProperties = peopleManager.getMyProperties();

clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(/*handlers*/);

Most likely, it is the how you are getting clientContext in this case. If the client context isn't loaded correctly first, then peopleManager will be undefined. Here is a link that most likely will address your issue:

SP.ClientContext.get_current() returning undefined objects

Beyond this, I cannot provide more information unless I see the whole code.

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

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

  function loadUserData() {
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var userProperties = peopleManager.getMyProperties();
    //....
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top