April 24, 2012

Automatically refresh SharePoint page, but not in edit mode!

Task

Task was to automatically refresh SharePoint page every 5 minutes. Simple task, but can frustrate content editors if not done properly.

Thoughts

Classic META REFRESH tag was out of the question, as it will refresh the page also when in edit mode. JavaScript examples floating around the web also seemed to ignore the requirement of not refreshing page when in edit mode.

Solution

Fortunately detecting the edit mode is a one liner in JavaScript, so just wrap your favorite page refresh JavaScript with the bold code below, save it in Content Editor Web Part, and you’re set! Happy refreshing!

<script type="text/javascript">
var reloadTimer = null;
var sURL = unescape(window.location.pathname);

function setReloadTime(secs)
{ if (arguments.length == 1)
   { if (reloadTimer) clearTimeout(reloadTimer);
       reloadTimer = setTimeout("setReloadTime()", Math.ceil(parseFloat(secs)*1000));
   }
   else
   { reloadTimer = null;
     location.reload(true);
     window.location.replace( sURL );
   }
}

var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;

if (inDesignMode != "1")
{

     setReloadTime(300);
}
</script>

Original refresh JavaScript (the one not in bold) was borrowed from Drew’s blog article.

Technorati Tags:

2 comments:

  1. could you be more specific? I need make it, but step by step

    ReplyDelete
  2. Hi, this works great. One problem though... when editing a list item, the page still refreshes! Is there a variable that I can use to stop it from refreshing when editing lists?

    Thanks so much!

    ReplyDelete