0

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!

2 Answers 2

1

You can try this to get SPS-DistinguishedName for the current logged in user.

var userProfileProperties;
var context = SP.ClientContext.get_current()
var web = context.get_web();
var peopleManager = new SP.UserProfiles.PeopleManager(context);
userProfileProperties = peopleManager.getMyProperties();
context.load(userProfileProperties);
context.executeQueryAsync(onRequestSuccess, onRequestFailure);

function onRequestSuccess(){
    var distinguishedName = userProfileProperties.get_userProfileProperties()['SPS-DistinguishedName'];
    console.log(distinguishedName);
    var currentUserAccount = distinguishedName.substring(3).split(',')[0];
    console.log(currentUserAccount);
}

function onRequestFailure(sender, args) { 
    console.log( args.get_message());
}
4
  • Thanks, worked like a charm! What was the source of my error? I would like to understand what was my flaw here.
    – sp4ce
    Commented Nov 7, 2018 at 8:47
  • executeQueryAsync() has 2 function arguments - success and failure. Whatever you want to do with the data (successful data or error) returned after executing must be written in these 2 functions. What you did works sometimes in browsers but in actual code it wont. You mentioned you tried it line-by-line in browser. Did you try on IE? Commented Nov 7, 2018 at 8:50
  • Also, you were using "getPropertiesFor()" which is a function you can use to get user profile properties of another user using their loginName. SharePoint has given the function to get current logged in users properties "getMyProperties()", so why not use that. Commented Nov 7, 2018 at 9:00
  • Didn't know the getMyProperties() function. Thanks for the insights and the clarification!
    – sp4ce
    Commented Nov 8, 2018 at 10:29
0

Add the following code into content editor web part in SharePoint page.

<script type="text/javascript" language="javascript">
ExecuteOrDelayUntilScriptLoaded(getUserProperty, "sp.js");
var userProfileProperties;
function getUserProperty(){ 
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var peopleManager = new SP.UserProfiles.PeopleManager(context);
    userProfileProperties = peopleManager.getMyProperties();
    context.load(userProfileProperties);
    context.executeQueryAsync(onRequestSuccess, onRequestFailure);
}


function onRequestSuccess(){
    var distinguishedName = userProfileProperties.get_userProfileProperties()['SPS-DistinguishedName'];
    console.log(distinguishedName);
    var currentUserAccount = distinguishedName.substring(3).split(',')[0];
    console.log(currentUserAccount);
}

function onRequestFailure(sender, args) { 
    console.log( args.get_message());
}
</script>

In SharePoint 2013/Online, we can use REST API to achieve it. The following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
    //execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            var distinguishedName;
            var properties = data.d.UserProfileProperties.results;   
            for (var i = 0; i < properties.length; i++) {
                var property = properties[i]; 
                if (property.Key == "SPS-DistinguishedName"){
                    distinguishedName = property.Value;
                }   
            }
            var currentUserAccount = distinguishedName.substring(3).split(',')[0];
            console.log(currentUserAccount);
        },
        error: function (data) {
            //alert("Error");
        }
    });
});
</script>
4
  • You used my working answer in your code. You could at least up-vote it before using which would not make me feel I wasted my time writing that piece of code. Commented Nov 7, 2018 at 7:18
  • @harshalgite Your code is not completed, and add the full tested code. I suggest the user using REST API in SharePoint 2013.
    – LZ_MSFT
    Commented Nov 7, 2018 at 8:01
  • Why would you loop through the properties where you can simply use the following line instead of the whole for loop. var distinguishedName = properties ["SPS-DistinguishedName"] If it doesnt work for you then can you please mention the error you get? The best way to write a code is to avoid loops wherever you can, especially in client scripting. Commented Nov 7, 2018 at 8:56
  • The var distinguishedName = properties ["SPS-DistinguishedName"] will not works.
    – LZ_MSFT
    Commented Nov 7, 2018 at 9:04

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.