3

I get the mentioned error message when i try to display the User Profile properties by using JSOM(Java Script Object Model) Error: Unexpected response data from server. The error occurs for around 1 min or 5-6 refresh , after that it works as expected . What could be the reason ? Please find the code below:

<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() {

        // Replace the placeholder value with the target user's credentials.
        var targetUser = "VMDEV\\XXXX";

        // 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.getPropertiesFor(targetUser);

        // 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() {

        // Get a property directly from the PersonProperties object.
        var messageText = " \"DisplayName\" property is "
            + personProperties.get_displayName();

        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['FirstName'] != "")
        {
            messageText += "<br />\"FirstName\" property is "
                + personProperties.get_userProfileProperties()['FirstName'];
        }
        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['Department'] != "") {
            messageText += "<br />\"Department\" property is "
                + personProperties.get_userProfileProperties()['Department'];
        }

        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['WorkPhone'] != "") {
            messageText += "<br />\"WorkPhone\" property is "
                + personProperties.get_userProfileProperties()['WorkPhone'];
        }

        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['SPS-Department'] != "") {
            messageText += "<br />\"SPS-Department\" property is "
                + personProperties.get_userProfileProperties()['SPS-Department'];
        }


        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['Fax'] != "") {
            messageText += "<br />\"Fax\" property is "
                + personProperties.get_userProfileProperties()['Fax'];
        }

        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['HomePhone'] != "") {
            messageText += "<br />\"HomePhone\" property is "
                + personProperties.get_userProfileProperties()['HomePhone'];
        }

        // Get a property from the UserProfileProperties property.
        if (personProperties.get_userProfileProperties()['CUS-CSIFS'] != "") {
            messageText += "<br />\"WXY-CSXYZ\" property is "
                + personProperties.get_userProfileProperties()['CUS-CSIFS'];
        }

        $get("results").innerHTML = messageText;
    }

    // This function runs if the executeQueryAsync call fails.
    function onRequestFail(sender, args) {
        $get("results").innerHTML = "Error: " + args.get_message();
    }
</script>
2
  • WXY-CSXYZ is a custom property , but it doesnt seem to be working . How do i access the custom property using JSOM ?
    – Nikhil J
    Commented Nov 5, 2013 at 22:48
  • The error occurs for around 1 min or 5-6 refresh , after that it works as expected . What could be the reason ?
    – Nikhil J
    Commented Nov 6, 2013 at 3:54

1 Answer 1

0

You can specify the property names which you want to fetch. Just include your custom property in the profilePropertyNames array:

$(document).ready(function(){                   
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js'); 
  });

  var userProfileProperties = [];

  function loadUserData(){

    //Get Current Context   
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Properties to fetch from the User Profile
    var profilePropertyNames = ["PreferredName","PictureURL"];  

    //If you are on On-Premise:
    var targetUser = "VMDEV\\XXXX";

    //Create new instance of UserProfilePropertiesForUser
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);

    //Execute the Query.
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onSuccess, onFail);

  }

  function onSuccess() {

    var messageText = "\"Preffered Name\" property is " + userProfileProperties[0];
    messageText += "\"PictureURL\" property is " + userProfileProperties[1];

    alert(messageText);

  }

  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  }

For more details, see my blog here

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.