0

I'm trying to get the user properties of the current user using javascript.

In C# i can easily do it using event receivers on itemadding. However i want to use javascript in order to get the user properties and it doesn't seem to work. I keep getting errors.

Here is the code :

 function getCurrentUserInformation() {
    var context = new SP.ClientContext.get_current();

    var currentUser = context.get_web().get_currentUser();

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

    var userProperties = ["InitialLeaveDays", "LeaveDays"];

    var userProfilePropertiesForUser = new UserProfilePropertiesForUser(context, currentUser, userProperties);

    context.load(userProfilePropertiesForUser);

    context.executeQueryAsync(onRequestSuccess, onRequestFail);

}

I'm getting error on peopleManager = new SP.UserProfiles.PeopleManager(context); stating that can't get of undefined or null . Don't know what is wrong .

Any ideas?

Thanks!

1

You are not passing the correct user information. You need to pass the actual user login name. Also, you need to wait for the SP.UserProfile.js to be loaded before you execute your getMyProperties function.

Add/Ensure the below references in your sharepoint hosted app page:

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.UserProfiles.js"></script>

Try the below code:

function getUserProperties() {
    var userProfileProperties;

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

    var currentUser = clientContext.get_web().get_currentUser();

    clientContext.load(currentUser);
    clientContext.executeQueryAsync(function(){
        var loginName = currentUser.get_loginName();

        var profilePropertyNames = ["InitialLeaveDays", "LeaveDays"];
        var userProfilePropertiesForUser = 
            new SP.UserProfiles.UserProfilePropertiesForUser(
                clientContext,
                loginName,
                profilePropertyNames);

        userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);

        clientContext.load(userProfilePropertiesForUser);
        clientContext.executeQueryAsync(function(){
            console.log("initial leave days :" + userProfileProperties[0]);
            console.log("leave days :" + userProfileProperties[1]);
        }, function(sender, args){
            console.log(args.get_message());
        });

    },function(sender, args){
        console.log(args.get_message());
    });
}

Also, ensure that your sharepoint hosted app has permission to read user profiles as below in your appmanifest.xml file:

enter image description here

3
  • Thanks for the reply . However the command SP.SOD.executeFunc won't execute my function . The breakpoint never hits my function. – shak Oct 11 '17 at 7:05
  • Please check the updated answer – Gautam Sheth Oct 11 '17 at 7:15
  • Thanks a lot ! It worked! Much appreciate it for your help – shak Oct 11 '17 at 12:35

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.