2

I'm trying to get user properties on newform but i get this error :

JavaScript runtime error: Unable to get property 'PeopleManager' of undefined or null reference

This is the Schema.XML of my list :

<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" JSLink="~site/Scripts/jquery-1.9.1.min.js|~site/Scripts/sputility.js|~site/Scripts/GetRemainingDays.js"/>

And here is my code to run GetRemainingDays.js :

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
alert("test");

function initializePage() {
$(document).ready(function () {
    alert("jquery");
    SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties,    'SP.UserProfiles.js');

    //getUserProperties();
});
}

function getUserProperties() {

//var clientContext = new SP.ClientContext.get_current();
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle("Settings");
var listItem = list.getItemById(1);


clientContext.load(listItem);
clientContext.executeQueryAsync(function () {
    if (listItem.get_item('ManagerSource') == 'O365 Active Directory') {
        alert(listItem.get_item('ManagerSource'));
        getO365Properties();
    }
    else
        if (listItem.get_item('ManagerSource') == 'Managers List') {
            alert(listItem.get_item('ManagerSource'));
            getManagerListProperties();
        }

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


function getO365Properties() {
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]);
        //alert("Initial Leave Days: " + userProfileProperties[0]);
        //alert("Remaining Leave Days: " + userProfileProperties[1]);
        var initialLeaveDays = userProfileProperties[0];
        var remainingLeaveDays = userProfileProperties[1];
        document.getElementById("label_leaveDays").innerHTML = "Your   remaining leave days are: " + remainingLeaveDays;
    }, function (sender, args) {
        console.log(args.get_message());
    });

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

Error happens on line var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

in getO365Properties function.

Any ideas how to correctly include SP.UserProfiles.js?

1 Answer 1

1

After some trying, I found that this works whereas SP.SOD.executeOrDelayUntilScriptLoaded(...) does not.

SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', getUserProperties);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.