Code gives error 2 times out of 10 attempts to refresh the page.
We get error as:
Request failed, unexpected responce from server. The status code of response is '0'. The status text of response is ''. Stacktrace is null.
Tried to place it on master page before head tag closes.
Also tried placing it at beginning of body tag.
Also tried using it on user control and registering this control on masterpage.
Code:
<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">
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") {
var div = document.getElementById('suiteBarLeft');
div.setAttribute("style", "background-color: red;");
}
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
//should I use window.reload here?
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
I have strong feeling that it has something to do with MDS ?