Adding an Auto-Refresh Content Editor Web Part

Written by MMMan on. Posted in JavaScript, SharePoint

The Challenge

You want to create a dashboard which will automatically refresh itself on a regular basis

The Solution

This is easily accomplishable using a SharePoint Web Part and little bit of scripting, JavaScript in this case, to be precise.

How to Accomplish This Task

Start by going to the page you want to edit, and begin editing the page (you will obviously have to have rights to do this).

051711 0506 AddinganAut1 Adding an Auto Refresh Content Editor Web Part

Add a Web Part to your page (you should already be in a mode like below).

051711 0506 AddinganAut2 Adding an Auto Refresh Content Editor Web Part

Choose the “Media and Content” Category, “Content Editor” Web Part, and add it to any part of the page (such as the “Header”).

051711 0506 AddinganAut3 Adding an Auto Refresh Content Editor Web Part

Select the web part and choose “Edit Web Part” from the drop down. A menu will appear on the top right hand side of the page, you may need to scroll over/up to find it.

051711 0506 AddinganAut4 Adding an Auto Refresh Content Editor Web Part

Set the Title to “Refresh the Web Page”.

051711 0506 AddinganAut5 Adding an Auto Refresh Content Editor Web Part

Set the Chrome Type to “None”.

051711 0506 AddinganAut6 Adding an Auto Refresh Content Editor Web Part

Click OK to accept those changes. Then you need to add the script to the web page. To do this, you start by clicking in the section “Click here to add new content”.

051711 0506 AddinganAut7 Adding an Auto Refresh Content Editor Web Part

A cursor will appear in the content area, however, don’t type anything in the content area, instead simply go to the ribbon and under Editing Tools -> Format Text, select the HTML drop down, and choose “Edit HTML Source”.

051711 0506 AddinganAut8 Adding an Auto Refresh Content Editor Web Part

In the HTML Source box, enter the following JavaScript code.

<script type="text/javascript">
function refreshPage()
{
   window.location = window.location;
}
setTimeout(refreshPage, 300000);
</script>

Basically, this script is calling a function after 5 minutes (1000 milliseconds * 60 seconds * 5 minutes = 300000 – this can be any value you choose, just do the math right), which will refresh the page (without showing the annoying, “Are you sure you want to resubmit this page” message that appears in IE when using “window.location.reload”).

Finally, stop editing the page and you will be all done.

051711 0506 AddinganAut9 Adding an Auto Refresh Content Editor Web Part

Your page should now have a hidden Web Part which will refresh the page every 5 minutes (or whatever number you should choose).

 

Incoming search terms:

  • auto refresh sharepoint page

Trackback from your site.

Comments (10)

  • CStone

    |

    It doesn’t work for all applications, but the Ajax refresh availabe on web-parts is also a great way make sure the information is up to date.

    Doesn’t require any code and only refreshes the specific web-part data instead of doing a full page refresh so it’s easier on the server.

    Reply

  • Mary

    |

    but not every web part has an Ajax refresh, right? or is this something we can turn on/off

    Reply

  • MMMan

    |

    No, the ajax refresh on web parts isn’t available universally, plus it will only update the particular web part, not an entire page. The idea for this post was to provide a means to force a page refresh, not a web part refresh.

    Reply

  • GGGman

    |

    I messed up and put a typo in the script for the refresh rate. Meant to put 15000 but I put 1500. Now the page refreshes so fast that I can’t get to where I can re edit the web part to fix that. Is there another way to edit a web part or do I have to rebuild the page? Thanks for the help.

    Reply

    • MMMan

      |

      You should be able to edit this page using SharePoint Designer. Unless it’s a publishing page, in which case, you could try to disable JavaScript in your browser and edit it that way.

      Reply

      • GGGman

        |

        That worked. Thank you very much.

        Reply

  • Ganesh Bairagi

    |

    Hi,

    Thanks for your help,it really helpful for me.

    Reply

  • B Baker

    |

    How do you remove embedded code from sharepoint 2013?

    Reply

    • MMMan

      |

      Have you tried deleting the web part that the code is embedded in?

      Reply

  • Jeremy

    |

    Great post – worked like a dream!

    Reply

Leave a comment