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?

Bishnu Das
  • 517
  • 2
  • 8
  • 21
  • 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 Das Jun 24 '14 at 16:07

4 Answers4

3

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);
Garima
  • 3,024
  • 1
  • 16
  • 19
  • I've followed this article. Still no success – Bishnu Das 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 Das Jun 25 '14 at 05: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 06:14
  • check this http://msdn.microsoft.com/en-us/library/office/jj163864(v=office.15).aspx#bkmk_AppPerms – Garima Jun 25 '14 at 06:16
  • The app now works when opened in its own page. I mean clicking on the app icon from Site Contents. But I try to place it on a page it block cross domain requests. Since the app web is different from host web. Would that, anyway, be possible to get through this way? – Bishnu Das Jun 25 '14 at 13:49
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*/);
wjervis
  • 5,738
  • 24
  • 45
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.

Brandon C.
  • 723
  • 7
  • 10
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();
    //....
  }
Aveenav
  • 4,199
  • 1
  • 14
  • 13