SharePoint Development: Re-Usable Data View XSL Templates
An often overlooked feature provided by SharePoint Designer is the ability to link an external XSL Template and associated Style Sheets to the a Data View Web Part (DVWP), allowing multiple Data Views to be included on different pages and Sharepoint sites that will all be rendered using the layout and styles specified in the centralised XSL Template. For example, you may have spent a large amount of time customising the styles of a Data View Web Part to include conditional formatting and many styles that are not core to SharePoint or the default styles of a Data View.
Instead of copying the entire Data View Web Part including the embedded custom XSL Style Sheets and Templates to each page that you want to display the data view, you can export the XSL template and Stylesheet to a separate .XSL file that can be stored in SharePoint, then used to apply formatting to multiple data view web parts.
An advantage to exporting and centralising custom XSL templates and style sheets that are used on multiple data View Web Part instances accross a site collection is that you can easily make changes to the layout or styles used. Changes will automatically be applied to all Data View Web parts referring to the custom xsl template when a page is loaded containing a DVWP.
How to Extract a Data View XSL Template to be saved as a centralised file that can be used by multiple Data View Web Parts:
- Using SharePoint Designer, open or create a page containing a Data View Web Part.
- Customise the styles of the Data View Web Part using CSS, conditional Formatting, alternate layouts, etc. until you are happy with it's appearance and the data being displayed.
- Select the Data View Web Part from the Design View of the page, or find in the page source where the web part is located. Find the <xsl:stylesheet ...> element, within the parent <XSL> tag.
- Select and copy the contents of the <xsl:stylesheet ...> element, and paste into a new text file.
- Save the file with a .xsl file extension into SharePoint to a location that is accessible to all users who need to view pages containing the data view web parts.
Applying a Centralised XSL Template to a Data View Web Part:
- Using SharePoint Design, Open the SharePoint site that will contain a page with a Data View Web Part.
- (optional) If the site opened is different from the SharePoint site that contains the List or Library that the Data View Web Part is to list the contents of, open the "data Source Library" task pane, and select the "Connect to another library" option.
- (optional) Press the "Add" button to create a new connection to another SharePoint Site, then enter a name for the connection in the "Display Name" field and the address of the SharePoint site containing the source list into the "Location" field.
- (optional) Press OK to create the connection
- From the Data Source Library task pane, expand the group with the title of the data connection to a separate site created above, or the "Current Site" group to list resources available on the current site.
- Find the list that you wish to have displayed as a custom Data View Web part, and drag the list/library into a Web Part Zone on the page. SharePoint Designer (SPD) will then add a List View Web Part to the page that is connected to the source list/library.
- Right Click on the List View Web Part, and select the "convert to XSLT Data View" option. This will convert the List View Web Part, including any view sorting, grouping, filter, etc. criteria that was previously applied to the LVWP into a Data View Web Part.
- Open the "Data View Properties" dialog for the web Part, the select the "XSLT Source" tab.
- Press the "Browse" button, the browse to the location of the central XSL template and press "Open". If you have not yet extracted and saved the XSL Template centrally for re-use by multiple DVWPs, follow the steps above for extracting and saving an XSL Template from a Data View Web Part.
- Press "OK" to apply the changes.
The Data View Web Part will the be rendered using the styles and layout defined in the XSL Template file linked to from the Data View Web Part.
For more SharePoint Development tutorials, how to’s, development resources, news and important updates, see SharePoint Development.
Related:
- Custom Table Of Contents XSL Templates
- XSLT String Manipulation - Remove spaces in Field Value for use in URL
- XSL Data View Conditional Formatting Using SharePoint Designer
- SharePoint XSL String Replacement Function
Submit a review:
Login required.