For another application, I need to fetch a property of the current logged in user profile in sharepoint. (SPS-DistinguishedName
) I've got this code, which works perfectly, pasted line by line into the chrome console.
var context = SP.ClientContext.get_current()
var web = context.get_web();
var user = web.get_currentUser();
var peopleManager = new SP.UserProfiles.PeopleManager(context);
context.load(user);
context.executeQueryAsync();
var personProperties = peopleManager.getPropertiesFor(user.get_loginName());
context.load(personProperties);
context.executeQueryAsync();
var accountdn = personProperties.get_userProfileProperties()['SPS-DistinguishedName'];
var currentUserAccount = accountdn.substring(3).split(',')[0];
But when I paste it altogether or use it as a block in the sharepoint site, I get the following error.
Uncaught Error: The property or field 'UserProfileProperties' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. at Function.Error.create (ScriptResource.axd?d=RB0pHBOM19TYuwxkj7EicKM89EIZ-Z29Jtd3sVdUBNgO-_DgIemCB0PCVtzZpKSdXKaI56C2Zj5_uiDNnnsS7r0XuWZ7y2ZGRztgkcjDBPbsUYB9o1tJvKc69Z84Jf9_PTBDeE1maXFsUyQ1Z63DsJOl1rEOsdza3Ni0mEbCvOOeUKygki2EL3uPm-I5wuHZ0&t=ffffffffcd368728:5) at SP.UserProfiles.PersonProperties.checkUninitializedProperty (SP.Runtime.js:2) at SP.UserProfiles.PersonProperties.get_userProfileProperties (SP.UserProfiles.js:1) at :11:34
Any idea which causes this? What can I do to avoid this error?
Thanks in advance and kind regards!