Second Life of a Hungarian SharePoint Geek

July 25, 2011

Triggering an asynchronous refresh from code

In one of my former posts I’ve demonstrated how to modify the list item properties from an ECB custom action using the client object model.

You might have noticed there that after the modification I reloaded the list content by setting the page location to itself:

window.location.href = window.location.href;

It means that all of the page content is reloaded that may be not optimal given that SharePoint 2010 lists support asynchronous updating.

But how can we achieve this functionality from client side code? To understand that, we should first enable the manual refresh button in the web part properties.

(Note: Checking the ‘Show Manual Refresh Button’ checkbox will check the ‘Enable Asynchronous Update’ checkbox after applying the settings.)

image

Then let’s see what HTML code is injected as the result of this setting (most important part is highlighted with bold):

<a href=”javascript:” onclick=” javascript: __doPostBack(‘ctl00$m$g_e9bf1559_32e7_40ea_88fa_3aced214cc97$ctl02′,’cancel’);return false;”><img src=”/_layouts/images/staticrefresh.gif” id=”ManualRefresh” border=”0″ alt=”Click here to refresh the data view.” /></a>

In the code above the value ‘e9bf1559_32e7_40ea_88fa_3aced214cc97’ is the ID (Guid) of the list having the leading and trailing curly brackets trimmed and hyphens replaced with underscore.

We can reproduce that for the list displayed by the current page using a code like this:

  1. var selectedListId = SP.ListOperation.Selection.getSelectedList();
  2. var convertedListId = selectedListId.toLowerCase().replace(“-“, “_”).replace(“{“, “”).replace(“}”, “”);
  3. var controlId = ‘ctl00$m$g_’ + convertedListId + ‘$ctl02’;
  4. __doPostBack(controlId, ‘cancel’);

Note: The above solution works for my current SharePoint installation as long as at least the ‘Enable Asynchronous Update’ checkbox is checked at AJAX Options. There is no guarantee that the structure of the control ID remains the same in forthcoming cumulative updates / service packs, or if you add other web parts to the page.

The code relies on the client object model when determining the list ID by calling SP.ListOperation.Selection.getSelectedList(), so the sp.js file must be loaded before the script executes. See my former post about how to use ExecuteOrDelayUntilScriptLoaded to enforce that.

In my forthcoming post I will illustrate this technique via a complete example.

Advertisements

1 Comment »

  1. Great thanks for putting this together, very useful.
    I’d use normal (double) quotes.. it makes it easier to cut n paste 😉

    Comment by mord — January 23, 2015 @ 10:15


RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: