Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

We are using js code to access user profile properties on an application page.

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.

Code: ( ContentPlaceHolderID="PlaceHolderAdditionalPageHead" )

      <SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>
<script type="text/javascript">
    var personProperties;

    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

    function getUserProperties() {

        // Get the current client context and PeopleManager instance.
        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

        // Get user properties for the target user.
        // To get the PersonProperties object for the current user, use the
        // getMyProperties method.
        personProperties = peopleManager.getMyProperties();

        // Load the PersonProperties object and send the request.
        clientContext.load(personProperties);
        clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
    }

    // This function runs if the executeQueryAsync call succeeds.
    function onRequestSuccess() {

        if (personProperties.get_userProfileProperties()['MyCustomProperty'] == 'True') {

            window.location("http://mysite:9334");

        }

    }

    // This function runs if the executeQueryAsync call fails.
    function onRequestFail(sender, args) {

        window.location.reload();

    }


</script>

What could be possible cause, I have used executeOrDelayUntilScriptLoaded..

share|improve this question
add comment

1 Answer 1

I think this row

if (personProperties.get_userProfileProperties()['MyCustomProperty'] == 'True')

should be

if (personProperties.get_userProfileProperties().MyCustomProperty == 'True')

(idea from http://www.c-sharpcorner.com/UploadFile/93cb27/get-user-properties-in-sharepoint-online/)

Also, you should probably use

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   // Make sure PeopleManager is available 
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
    }
}

from Are the SP.js and SP.UserProfiles.js preloaded in SharePoint? instead of using executeOrDelay since these scripts are on-demand scripts.

Edit Seems userProfileProperties is not loaded, how about:

    // getMyProperties method.
    personProperties = peopleManager.getMyProperties();
   upProperties =  personProperties.get_userProfileProperties();
    // Load the PersonProperties object and send the request.
    clientContext.load(upProperties);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);

And then work with upProperties in the onRequestSuccess method

share|improve this answer
    
I vl try this tomorrow. Thanks –  variable Mar 24 at 15:50
    
chagned. still the same issue. –  variable Mar 25 at 6:36
    
See update above –  Robert Lindgren Mar 25 at 7:07
    
Is it that the js is not loaded or property is not loaded? I even tried to use executeordelay for sp.js as well as for sp.userprofiles.. –  variable Mar 25 at 7:14
    
Also, do I need to call the script reference tags? SharePoint:ScriptLink for sp.js and sp.userprofiles.js? –  variable Mar 25 at 7:17
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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