Programmatically enable Ajax Auto Refresh on a SharePoint XsltListViewWebPart

On of the capabilities of the XsltListViewWebPart used extensively by SharePoint 2010 is the automatic refreshing of list view data via Ajax. Used judiciously, this can be very useful for creating dynamic views of data, pseudo-dashboards or in my case, providing feedback of timer job processing via the UI.

To enable automatic refresh via the UI is simple, edit the web part properties, and switch on the ‘Enable Asynchronous Automatic Refresh’, set the refreshing interval (which must be no more frequent than 15 seconds) and save the web part.

image 

To achieve the same results via code is just as simple:

// update the XsltListViewWebPart to refresh automatically via Ajax
// get the webpart page and webpart manager
SPFile file = web.GetFile("Lists/<your list name>/AllItems.aspx");
SPLimitedWebPartManager wpManager = 
    file.GetLimitedWebPartManager(PersonalizationScope.Shared);

// get the XsltListViewWebPart...
// assumes the XsltListViewWebPart is the first webpart on the page
XsltListViewWebPart lvwp = (XsltListViewWebPart)wpManager.WebParts[0];
                
// set the webpart to autorefresh and set the interval
lvwp.AutoRefresh = true;
lvwp.AutoRefreshInterval = 15;

// save the changes
wpManager.SaveChanges(lvwp);

Enjoy…

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: