Programmatically enable Ajax Auto Refresh on a SharePoint XsltListViewWebPart
January 12, 2012 Leave a comment
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.
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…




