Article
6 comments

Client Side Rendering – In-place upgrade of SharePoint list views

When you migrate SharePoint 2010 to SharePoint 2013 or to Office 365 some of the views that was created in the previous version might look the same.
The user will get a mixed user experience because document libraries, switch between the old view styles to views that are configured for client side rendering. Via scripts those views can be recreate, but they also can be upgraded in-place.

Difference between Client Side and Server Rendering

For the end user the difference just results in a different look and feel. The most relevant stuff happens in the background. For example, when a view will be filtered the page reloads with a post-back. The complete page will be transferred to the client and render in the browser. This happens with server side rendering.
With client side rendering only the new data will be transferred to the client. This will be a smaller amount of data. The data will be fetched by a JavaScripts, prepared and render on the client. This result in less load on the server and more load on the client. The better the client, the faster the views loads. A fact that doesn’t really matter these days because modern devices are good enough to handle this client side javascript and rendering. If you like to know more about this check “introduction to client-side rendering in SharePoint 2013
by Ross Bradbrook.

Difference in the object model

The web part that renders the list views can be found in the object model as a XsltListViewWebPart. This web part has a property ServerRender. It’s pretty interesting that this document doesn’t have a documentation in the msdn but on the other hand it’s just a simple boolean. If it is set to true the web part is configured to server, rendering; if it is set to false client side rendering will be used.
Server-Side-Renderint

List view in classic SharePoint 2010 mode

List view in classic SharePoint 2010 mode

To upgrade the following code needs to be applied to the list.

using (SPSite site = new SPSite("http://someweb"))
{
  // open the web
  using (SPWeb web = site.OpenWeb())
  {
    // Fetch the list that should be upgraded
    SPList list = web.Lists.TryGetList("Somelist");

    if (list == null) {
      throw new SPException("List doesn't exist");
    }
                    
    // Loop throught all the lists
    foreach (SPView listView in list.Views)
    {
      // Request the file of the view because we like
      // to change the web part
      SPFile file = web.GetFile(listView.ServerRelativeUrl);

      if (file.Exists)
      {
        // Open the web part manager so that we can access
        // the web part on the list view
        using (SPLimitedWebPartManager wpManger =  file.GetLimitedWebPartManager(PersonalizationScope.Shared)){
          // Fetch the first web part on the view
          // This is only capable if you are inside a list
          XsltListViewWebPart viewWebpart = 
              wpManger.WebParts[0] as XsltListViewWebPart;

          // Check if the web part could be found
          if (viewWebpart != null)
          {
            // Setting the web part to render on the client
            viewWebpart.ServerRender = false;
                                    
            // Featch the view of the web part
            // this is actually the view itself
            SPView view = viewWebpart.View;
                                    
            // Set the correct rendering template
            // on the JS Link
            view.JSLink = "clienttemplates.js";
                                    
            // Update the view
            view.Update();
                                    
            // Finally save the web part on the view page
            wpManger.SaveChanges(viewWebpart);
                                
          }
        }
      } 
    }
  }
}

After that the list view is upgraded an will be displayed correctly using SharePoint 2013 client side rendering.

List-view-with-client-side-rendering-enabled-SharePoint-2013

List view after the upgrade have been applied

Cases this upgrade mechanism needs to be applied

Actually over past few weeks I’ve seen a couple of list view that was still rendered as they were in SharePoint 2010. In this case one of those properties wasn’t applied correctly.
You might have some code in your farm solution that adds Xslt List View Web Part to web part pages. Then those properties should be added.
I case you migrate your SharePoint 2013 to Office 365 it should also be part of your migration strategy to make all views look and work the same.
A tool that can be really supportive when you are planing an upgrade to the most recent version is the SharePoint Code Analysis Framework (SPCAF). Along with loads of code quality and migration checks, it warns you about possible issues that should be changed during a version upgrade of a farm solution.

6 Comments

  1. Hi, is it possible to achieve this with a webpart page ? after a migration all webparts on pages are rendered with the old style view. If I edit them individually with the cleinttemplate.js the view appears correct but we have many many subsites and would like to change all webparts on all the subsites home pages so they use the clienttemplate.js.

    Thanks

    Reply

    • Hi Steve,

      yes you should be able to do it for web part pages too.
      XsltListViewWebPart viewWebpart = wpManger.WebParts[0] as XsltListViewWebPart;
      In my code this just takes the first web part on the page. What you need to do is over all web parts and check if they are of type XsltListViewWebPart.

      /Stefan

      Reply

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.