Domanda

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!

È stato utile?

Soluzione

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());
}

Altri suggerimenti

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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top