2

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?

  • As Brandon stated in his answer, we need more context. How are you creating clientContext? Is this a SharePoint Hosted app? If so, you need to use the SP.RequestExecutor.js library. – wjervis Jun 24 '14 at 14:13
  • Yes, the app is a SharePoint hosted app. I am getting the client context using SP.ClientContext.get_current(). I suppose the this is getting loaded currectly. Because I tried loading get_currentUser(). And I am able to get email, loginName etc. – Bishnu D. Jun 24 '14 at 16:07
2

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);
  • I've followed this article. Still no success – Bishnu D. Jun 24 '14 at 16:20
  • Have you given read permission to user profile in appmanifest.xml? – Garima Jun 24 '14 at 17:24
  • have added a working code.. – Garima Jun 24 '14 at 17:58
  • I've now given read permission to User Profile( Social ). It seems working now. But at the permission page it says "Sorry, only tenant administrators can add or give access to this app.". Do you believe this will not raise any issue? – Bishnu D. Jun 25 '14 at 5:33
  • This is by default nature of SP. For some of the services (like user profile) only tenant admin can give permission to the app (Click Trust It). There is no other way. – Garima Jun 25 '14 at 6:14
2

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*/);
  • You don't need SP.RequestExecutor.js for this. – Garima Jun 24 '14 at 17:49
  • What should appweburl and hostweburl be? – Emaborsa May 17 '17 at 7:11
0

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.

  • I've added the detailed code – Bishnu D. Jun 24 '14 at 16:17
0

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

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.