1

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 ?

2
  • 1
    What if it fails every request.. you will cause an infinity loop. Commented Mar 24, 2014 at 11:59
  • Yes, so what do you suggest? Is there any good practice to handle such situations? Commented Mar 24, 2014 at 12:09

2 Answers 2

2

Instead of reloading the page I would suggest that you implement some actual error handling in that function.

So instead of:

function onRequestFail(sender, args) {
    window.location.reload();
}

You should consider doing something like this:

function onRequestFail(sender, args) {
    // Add OOTB status message to the page.
    var status = SP.UI.Status.addStatus(args.get_message());

    // Give it the color 'red' to emphasis an error has happened.
    SP.UI.Status.setStatusPriColor(status, 'red');

    // Log it in the client for debugging.
    console.log(args.get_message());
    console.log(args.get_stackTrace());
}

There are many ways to handle this, this is just my preference.

1
  • I am changing the masterpage css style tags of divs based on loggedin user property. So your solution will not be feasible. Commented Mar 25, 2014 at 4:22
2

This is definitely not a good practice. As someone has already pointed out, if the request keeps failing, you'll end up with an infinite loop.

You said you don't know why the request sometimes fails. In order to investigate the underlying cause, the args arguments has all that you need:

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
5
  • Yes the error is: Request failed, unexpected responce from server. The status code of response is '0'. The status text of response is ''. Stacktrace is null Commented Mar 24, 2014 at 12:35
  • How and where are you triggering the request? Can you show us more code? Also, did you try to use the network capture tool in Chrome or IE ? Commented Mar 24, 2014 at 12:42
  • updated code... I am following good practice. But I dont know why this error comes occasionally. Commented Mar 24, 2014 at 12:44
  • @NachiketKamat did you check the usl log to see if it tells anything more about the error? Commented Mar 24, 2014 at 12:59
  • Also, try this: sharepoint.sureshc.com/2013/07/… Commented Mar 24, 2014 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.