Refreshing a Web Part Without a Postback Using jQuery’s AJAX Function

I’ve been working on several projects with good old SharePoint 2007 (MOSS). jQuery and jQueryUI sure can spice it up nicely. Recently, I decided I wanted to refresh the contents of a Data View Web Part (DVWP) based on a user selection – a simple click on one of a big set of links – but I wanted to avoid that clunky postback.

SharePoint 2010 has the ManualRefresh capability baked right into DVWPs, but not SharePoint 2007. Besides, I wanted both the trigger for the refresh and the UX to work differently than the rather utilitarian (and never used in my experience) out of the box feel that 2010 gives you.

This is the script I came up with. In this case, I decided to show a nice message in a modal dialog using jQueryUI while I waited for the .ajax() call to complete, as I’m using an AggregateDataSource that is querying several pretty large lists. This means it takes a few seconds to come back, and the dialog saying what’s going on – not just a spinner – makes the UX more palatable.

Refreshes an element's contents on a user action, showing a modal dialog during the refresh
elementId  The id of the container element
qs         The Query String to append to the current URL
title      The title to show for the dialog
msg        The message to show in the dialog

function refreshElement(elementId, qs, title, msg) {

  var elementObj = $("#" + elementId);
  var infoDialog = $("<div><div>" + msg + "</div><div class='aaa-please-wait'></div></div>").dialog({
    open: function(event, ui) {
      $(".ui-dialog-titlebar-close").hide();  // Hide the close X
      $(this).css("overflow-y", "visible");   // Fix for the scrollbar in IE
    autoOpen: false,
    title: title,
    modal: true

  elementObj.fadeOut("slow", function() {
      async: false,
      url: window.location.pathname + qs,
      complete: function (xData) {
        newHtml = $(xData.responseText).find("#" + elementId).html();
  }).fadeIn("slow", function() {

I made the function pretty generic so that I could pass in the four arguments and use it in multiple locations if I needed to. You could really pass in the id of any element in the page. In the instance I built it for, I was passing a new value on the Query String based on what link the user clicked on, but fetching the same page with AJAX. The DVWP was set up to return different data based on the Query String parameter value.

Keep in mind that both jQuery and jQueryUI must be loaded for this to work.

The CSS for the body of the dialog is pretty simple, but here it is. The sizing worked well for me based on the image I decided to display.

.aaa-please-wait {
  background-position:center center;

What do you think? Would this make a good addition to SPServices? If so, what other options would you like me to include? If I did add it to SPServices, I’d remove the dependency on jQueryUI, as I don’t want any outside dependencies other than jQuery itself.

Enhanced by Zemanta


  1. I haven’t tested it, but the code looks good. My only feedback on this code is to speed up the fade transitions or drop them altogether. Slow fades really make a UI feel, well, slow.

    If adding to SPServices (which I’m torn on; this isn’t strictly a web services function so a standalone jQuery plugin seems more appropriate), I’d extend the options so that it can use a modal or not (maybe put the loader gif over the web part itself with a white translucent overlay or something), has defaults for the title and msg values, allows for async: true, and provides an auto-refresh option that can be run on page load (which probably should require async: true). Definitely drop the jQueryUI dependency. The SPServices debug modal would work nicely for this with some CSS tweaks I think.

    • Josh:

      Thanks for the feedback.

      The fade transitions worked well for this particular use. It made the time it took to load the content feel like something was happening. At least that’s what the people who use it seem to feel so far. Adjustments may be needed.

      It’s definitely not a Web Service thing. I’m a little torn about adding non- Web Service stuff to SPServices, like you. However, as 2007 fades into the background and even 2010 is about to start thinking about retiremenet, it may be time to expand on the mission. I’m really not sure. I’ve added a new function in 0.7.2 which helps with People Pickers – SPFindPeoplePicker – but doesn’t have anything to do with Web Services, either. Bad idea? Maybe.

      On the other stuff, great ideas, as usual.


  2. Marc – if you have, say, 10 web parts on a page, this logic will refresh the whole page using AJAX and then your script use DHTML to update the particular web part. Wouldn’t that model use to much performance and bandwidth?

    I would expect that SharePoint could update web parts dynamically – one at a time. But to my knowledge this is not possible.

    Have you investigated creating web parts purely in jQuery UI? We’ve done a few web parts in this way – which can be updated dynamically one-by-one on the page. Quite powerful.

    • Morten:

      I used this script to refresh one Web Part in SharePoint 2007 on a user action. If there were 10 Web Parts on the page, I could call it to only act on the one Web Part I wanted to refresh.

      Building your own “Web Parts” with script (pretty easily donw by adding some markup and script into a Content Editor Web Part) is certainly a great idea in some cases. It’s really important to let the server do the work it is good at, though, and do work on the client side when it makes sense.


      • The initial solution was actually published 3 years ago. I wrote it for any kind of view, not just DVWP, and also to work cross-site. That’s why it adds some complications, because stuff such as calendar buttons and context menus will break and have to be disabled.

    • Ruben:

      Animated GIFs don’t play well in IE8 during synchronous operations, as you’ve noticed. I’ve never found a way around it.

      I’m starting to play around with jQuery promises, and I think that’s the way forward.


  3. I have a data view web part and a text filter with wild card search connected to it.
    I am not too sure if the code above can be applied on Office 365 online version inside a jQuery tab panel?
    If yes, can you please send some code examples?
    Is the element id for the div?

    Thank you for help!

  4. Hi Marc,

    This is not the first post of you which helped me a lot. So thank you very much your work.
    With this post I successfully refreshed my DFWP without refreshing the whole page and merged with SPCascadeDropdowns to filter and show summary informations on my SPS Online page.


      • Hello Marc,

        Could you help me please a little?
        I used the above method to refresh and then filter my page. However SPCascadedDropdown seems not working well for me with lists has more than 20 items in it.

        A have read a lot and fount that IE renders page different with 20+ items in it and for me it seems SPCascadedDropdowns not handle it. It can be seen well it renders as an input not a select field. Also it changes its name to “Field” instead of “Field Required Field”. I couldn’t make it to work so the best I could do with my actual knowledge is I found the SPComplexToSimpleDropdown function. This works well in IE 11. However in my filter text (in my list items) there are not only english characters (é, á, ö, ű …). The SPComplexToSimpleDropdown seems change these to unreadable characters (a rectangle with question mark in it) so the filter stopped working.

        Is this a charachter encoding issue? Do you have any idea what should I do?



        • Gabor:

          SPCascadeDropdowns should work with any combination of dropdown types, as noted in the documentation. If you’re seeing the ” Required Field” suffix, then you’ll need to be using SPServices 2014.01.

          As for the non-English characters, I’m not sure. If you’re still having problems, please post the details to the SPServices discussions and include your code.


  5. Thanks Marc!

    I know SPCascadedDropdown should work but it doesn’t. I’m using 2014.01 version.

    Will post it to the right forum.

    Thank you!


Have a thought or opinion?