SharePoint 2007 Auto Page Refresh on Workflow Completion (SOLVED)


Well, solved with some JavaScript that is.

This won’t be the ideal situation for everyone, but works for my immediate need and may well work for yours.  Use with caution!

The Problem:

Often in the SharePoint world we use a Workflow designed and deployed with SharePoint Designer to update a list field value.  Usually this is down when we want to include information from another list or something similar.

In my scenario, I was tasked with creating a simple Email Register that displayed a link with embedded subject and CC.  The subject needed an auto number included. The MSDN forums had the SharePoint auto number solution.

Once this was implemented I was on my way! Well, almost. Nasty SharePoint didn’t show the generated email link until AFTER the user refreshed the page (F5).

The Solution:

jQuery! (Really, where you expecting something else?)

Using the code below, I’ve setup an auto page refresh. Now, we do have some caveats:

  • List must be sorted so Newest item is always at the top.
  • Users must NOT be allowed to change that.
  • If the site is slow, the page may perform 2-3 (or more if really slow) automated page refreshes.
  • Test and act in kind.
  • The WorkFlow column must be visible

So that being said, the JavaScript.

Include a Content Editor WebPart on the page, use the Source Editor to paste in the code. NOTE: you will have to adjust the column (td:nth-child(3)) to match what you have in your list.

Basically, the script just checks for “In Progess” in the WorkFlow column, waits for 1 second and refreshes the page. The 1 second pause helps reduce multiple refreshes.

About the Selector:

.ms-listviewtable: I used a class as the ID was crazy long and gave me some issues. If you have more then one of these on your page, beware it will check all of them.

tr:nth-child(2): Move to the 2nd row. The 1st row is the Header (column titles).

td:nth-child(3): Move to the 3rd Column. (You will probably need to adjust this) This column will have a header value of the WorkFlow name.

a span: The text is wrapped in an anchor and span tag.

The Script:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
 $(document).ready(function(){
      var td = $(".ms-listviewtable tr:nth-child(2) td:nth-child(3) a span").html();
      if(td=="In Progress"){
           sleep(1000);
           window.location=window.location;
      }
 });
function sleep(ms)
 {
      var dt = new Date();
      dt.setTime(dt.getTime() + ms);
      while (new Date().getTime() < dt.getTime());
 }
</script>

And this does nicely for my needs. Hope it helps someone!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: