1

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..

2

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

13
  • chagned. still the same issue.
    – variable
    Mar 25 '14 at 6:36
  • See update above Mar 25 '14 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 '14 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 '14 at 7:17
  • Hard to tell, but you should use SP.SOD.execute as I demonstrate in my post (works for all types of SP scripts, both on-demand and not). I think your error though, is due to you not loading personProperties.get_userProfileProperties() in to the context before doing executeQueryAsync Mar 25 '14 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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