Tuesday, February 05, 2008

Auto-Refresh a SharePoint Page

This is more of a quick and dirty technique rather than an elegant, AJAX-based approach which might be more desirable.

Enabling your SharePoint page to refresh automatically after x seconds is as simple as adding a Content Editor web part and adding the following javascript to it in the source editor:

<script type="text/javascript" language="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 );
}
}

setReloadTime(30);
</script>

This script will reload the page ever 30 seconds. In order to hide the web part from the view on the page, set the following properties:
Height: 0px
Width: 0px
Chrome Type: none
Under advanced uncheck all advaanced properties.

That should do it...now your SharePoint page will auto-refresh every 30 seconds.

Good Luck...

27 comments:

  1. you're missing a slash from the script close tag. cheers though.

    ReplyDelete
  2. Thanks for catching my typo.

    ReplyDelete
  3. Every time it attempts to refresh, a dialog box appears telling me that IE needs to resend the information with buttons to 'retry' or 'cancel'...any way around this?

    ReplyDelete
  4. Every time it attempts to refresh, a dialog box appears telling me that IE needs to resend the information with buttons to 'retry' or 'cancel'...any way around this?

    ya, me too. Any help would be awesome!

    ReplyDelete
  5. What is happening is that the refresh is trying to post back form data again. You need to set the following:

    remove
    location.reload(false);

    replace
    window.location.replace...
    with
    window.location(sURL);

    Let me know if this works for you.

    ReplyDelete
  6. Thanks a lot!

    I changed location.reload(true);

    to
    location.reload(false);

    and made the 2nd change

    replace
    window.location.replace...
    with
    window.location(sURL);

    It works great, thanks for your help.

    ReplyDelete
  7. This works ok, but the problem is it makes my server run to a 100% everytime it refreshes.
    But is there a AJAX enabled refresh webpart for announcments available maybe ?

    ReplyDelete
  8. Thanks heaps for this.

    To get rid of the dialog box I had to change window.location.replace...
    to
    window.location=sURL;
    and remove the location.reload line completely.

    ReplyDelete
  9. That did it!!
    Had to change to:
    window.location=sURL;
    and remove the location.reload line completely.

    ReplyDelete
  10. This works but if I have a filter set on a column it resets the filter. Is there a way to keep the filter when it refreshs?

    ReplyDelete
  11. bump for above question

    ReplyDelete
  12. hi,
    i have created one webpart which shows slides for images.
    My Sharepoint Page load first time at that time i m not able to see the slides in my webpart....so i need to do is that after first sharepoint page load.... i need to refresh only that webpart (slides) after 5 second of first load of whole page.
    Is it possible for webpart.???

    ReplyDelete
  13. great post, thank you very much!!!

    ReplyDelete
  14. Great post! I was looking for this one, although I'd had to adjust the original code with window.location=sURL to avoid the pop-up prompt on refresh.

    ReplyDelete
  15. greate post, but I have the sameproblem as anonymous, I ahve a webaprt on the apge that has a calendar and if I change the date, I would like to keep that date efter the refresh. anyonw got any idea?

    ReplyDelete
  16. I was having a problem with sorting being lost on the page refresh. What I did to fix that problem was just to change var sURL = unescape(window.location.pathname);
    to
    var sURL = window.location;

    This preserves the sort since the sort variables are passed in the querystring.

    ReplyDelete
  17. Nice tip, if your page is _finished_. However, if you have a target usergroup allowed to edit the page, you will have a problem. Auto-refreshing a page in edit mode is, needless to say, extremely undesirable.

    ReplyDelete
  18. Hi, this is a great post. . very helpful. but is there an AJAX enabled refresh webpart? I want my data to get refreshed every 30sec automatically but do not want that webpart/page to blink...

    Please help...

    ReplyDelete
  19. This actually worked for me so I'm extremely grateful, half the blogs\posts on the internet about Sharepoint are rubbish and code snippets offered don't work so this makes a refreshing change...thank you.

    ReplyDelete
  20. Hey guys is there any way to use ajax in it.. I am using this snippet alog with a performance point webpart to update the KPI webpart.....and if users settings like collapsed and expanded items also get reset to orignal value....i.e all expanded or all collapsed.....

    ReplyDelete
  21. I've been trying all you people posted and still had problems, finally i resolved it with this simple script

    < meta http-equiv="refresh" content="60" >

    just added it into "source editor" and everything worked fine.

    ReplyDelete
  22. hmm... i just posted the script:

    < meta http-equiv="refresh" content="60" >

    i've forgot to tell you ppl, to not forget to remove the space's between the " < " and " > "

    =)

    ReplyDelete
  23. I am very new to all of this...I have a webpart that displays information from several another sources(printer/toner status) I would like to refresh the (printer/toner)status when a user loads the page, any ideas?

    ReplyDelete
  24. works great, thanks!

    BTW, setting it to 5 seconds to test is fun: on an old dev server it takes more than 5 seconds to get to the Source to change it!

    ReplyDelete
  25. I love the single line meta tag version - worked like a charm and doesn't consume much server cpu time.

    ReplyDelete
  26. receptor petra onwards tianzhong stocked holder revisited landfill doecke fragments generated
    semelokertes marchimundui

    ReplyDelete
  27. Nice piece of information... thanks for sharing...

    ReplyDelete