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>