SP.Ribbon.PageState

SharePoint page state is commonly used to perform specific actions based on the state. For instance, isInEditMode is used to determines whether the page is currently being edited. For more page state methods and their description please visit next MSDN link:

SP.Ribbon.PageState.Handlers Methods

Using next line in JavaScript you will be able to check whether edit state of a SharePoint page:

var inEditmode = SP.Ribbon.PageState.Handlers.isInEditMode();

In this blog post I will explain why this solution sometimes is not working and present the solution.

But sometimes this doesn’t work!

Even when the page was in Edit mode.  Using the browser development tools to check the return value after the page had loaded, it was correct, so we have a timing problem.

Solution

You could use next code to delay until sp.ribbon.js is loaded.

function myFunction() {
(SP.Ribbon.PageState.Handlers.isInEditMode() otherFunction());

setTimeout(myFunction, 1000);
}

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(myFunction, "sp.ribbon.js");
});

A cleaner solution if you would like to reuse this functionality in your code would be next code:

//checks wether page is in editmode

my.pageIsInEditMode = function () {

var deferred = $.Deferred();

ExecuteOrDelayUntilScriptLoaded(function () {

deferred.resolve(SP.Ribbon !== undefined && SP.Ribbon.PageState.Handlers.isInEditMode());

}, 'SP.Ribbon.js');

return deferred.promise();

}
 

And how this could be called on:

MyModule.pageIsInEditMode().done(function (isInEditmode) {

if (isInEditmode) {

otherFunction();

}else

{

//do nothing

}

}

I hope this solution helps you with your case!

Don’t forget to leave your comments and improvements to my code! Tnx

Leave a Reply

Your email address will not be published. Required fields are marked *