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

variable
  • 4,473
  • 13
  • 75
  • 139

1 Answers1

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

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
  • chagned. still the same issue. – variable Mar 25 '14 at 06:36
  • See update above – Robert Lindgren Mar 25 '14 at 07: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 07: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 07: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 – Robert Lindgren Mar 25 '14 at 07:51
  • I am following this link http://msdn.microsoft.com/en-us/library/office/jj920104(v=office.15).aspx#bk_examplePersonPropsObj. I was using above code on master page and it gave me regular errors as mentioned. However now I have created an user control (script is now in usercontrol) and put that on masterpage and the error have gone away. What do you think would be the problem? This will solve my issues but then I would like to know why it wont work occasionally on masterapage – variable Mar 25 '14 at 07:52
  • The difference can be the page you are on, maight be that SP.UserProfiles already gets read in on the page you are using now – Robert Lindgren Mar 25 '14 at 07:54
  • I always tested on home.aspx.. – variable Mar 25 '14 at 08:02
  • Then it might be a timing issue, where one of your codes where executed before all dependet scripts where loaded.. – Robert Lindgren Mar 25 '14 at 08:04
  • I have one question. Suppose we specify a script tag at start of body tag to change color of a div. The div is far ahead near end of body tag. Then, when does script get run. Does the html load 1st and then the script? Or is it (that script executes) in the order that it is present in body tag? – variable Mar 25 '14 at 08:09
  • This is not related to this question – Robert Lindgren Mar 25 '14 at 08:23
  • OK, just while testing, the same error has come back for the usercontrol on the master page too.. and the problem is only when refreshing the homepage, on other pages it works well. – variable Mar 25 '14 at 08:25
  • Im sure that it is related to the layouts/15/start.aspx appreaing in the URl – variable Mar 25 '14 at 09:43