Customize XSLT List View Web Part

The new list view web part in SharePoint 2010 is great because it allows you to customize it easily using XSLT stylesheets instead of CAML.

Although it is possible to add XSLT list view web parts via mark-up directly (either in the page directly or in elements file) you should not do so. The XSLT list view web part has no ‘Export’ option and the reason for this is that it should not be import in anyway. If you still want to add a web part via mark-up note these issues:

  • The value of the title attribute must be different from the display name of the list. Otherwise the web part will show ‘Untitled’ as title.
  • To reference the list use the ListUrl attribute. It is relative to the current web.
  • To set the Url for the title use the TitleUrl attribute. It is relative to the current location of the page.
  • To remove the check-boxes for selection (left column) put the TabularView=”FALSE” attribute on the View element.
  • When updating to a newer version of SharePoint (e.g. installation of service pack or CU update) the earlier created views might break.

One thing that is good to know is this: List view web parts added to pages via mark-up will always result into an error when editing the web part in page through the SharePoint UI or SharePoint Designer. In case it is required to be able to further customize the pre-added web parts at a later stage the list view web parts must be added by code or by using a View element in the module (and not mark-up). The only work-around is to remove the ‘failing’ web part from the page and add a new one.

The proper way to add XSLT list view web part is via the View element in a module:

<Module Name="Module1" Url="$Resources:cmscore,List_Pages_UrlName;">
  <File Url="default.aspx" NavBarHome="True" >
    <View List="Lists/Contacts" BaseViewID="1" WebPartOrder="1" WebPartZoneID="Header"/>
  </File>
</Module>

It seems essential to specify the BaseViewID even though the MSDN documentation specifies it as optional. Without a BaseViewID attribute I get a ‘Cannot complete this action’ error. The title of the created XSLT list view web part is always the list name. If you want to change that or any other property of the XSLT list view web part that is not exposed by the View element you can do this through the SharePoint object model. This is not ideal because it requires custom code.

Also note that there are (at least) to View elements in the MSDN documentation. The View Element (Site) and the View Element (List). The first one is used in modules and the latter on in list schema definitions. The View Element (List) contains a lot of child elements for backward compatibility, which are ignored with the XSLT list view web part in SharePoint 2010. These element are described here. An ok general introduction to the XSLT based rendering mechanism can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *