How to have a look behind the data source of the XsltListViewWebPart of SharePoint 2010

I want to create a custom field type in a SharePoint 2010 development project (which I want to publish as as “walkthrough” on this blog Smile )

Therefore I need to inspect the source XML data that is used for XSLT rendering on a lists view page, e.g. “AllItems.aspx”

This is the result:

image

You see here for each field (column): The source XML and the items field value behind the XML data.

For the demo above I created a new custom list. It has some fields:

  • Title : Single line of text
  • Choice Field : Choice field with 3 choice values
  • Date Field : Date only field
  • Lookup Field: Lookup field to the “Start Time” field of the standard Team Site “Calendar” list. (Additional field in view: “End Time”)
  • People Picker Field: People or Group field with “People only”.

I added a single item.

That’s the set.

For the lookup field you get this result XML data:

<FieldRef Name="Lookup_x0020_Field" Type="Lookup" FieldType="Lookup" Encoded="TRUE" DisplayName="Lookup Field" ID="da1fb516-3300-47e9-8734-33db5479876e" />

For the “Title” field you get this result XML data:

<FieldRef Name="LinkTitle" ListItemMenu="TRUE" Type="Computed" FieldType="Computed" DisplayName="Title" ID="82642ec8-ef9b-478f-acf9-31f7d45fbc31" ClassInfo="Menu" ListItemMenuAllowed="Required" LinkToItemAllowed="Prohibited" />Test Text

(This field is “Computed”. In the view you do not see the “Title” field itself. You see a “computed” field named “LinkTitle” instead.)

If I want to develop a custom field type I need to create a XSL transformation for the field data. Therefore I need to refer to the source XML data. But at development time I do not know the exact structure and content of the XML data. So I modified a file in the SharePoint hive….

NEVER do the following in a productive SharePoint 2010 farm! 

1. Look for the file “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATELAYOUTSXSLfldtypes.xsl”

2. Open it.

3. Look for this line:

  <xsl:template name="PrintField" ddwrt:dvt_mode="body" ddwrt:ghost="always">

4. Before this line insert this XSLT code:

1:   <xsl:template name= "ikarstein_nodes "> 
2:     <xsl:element name= "{name()} "> 
3:       <xsl:text><![CDATA[<]]></xsl:text> 
4:       <xsl:value-of select= "name() "/> 
5:       <xsl:text><![CDATA[ ]]></xsl:text> 
6:       <xsl:for-each select= "@* "> 
7:         <xsl:call-template name= "ikarstein_attrib "/> 
8:       </xsl:for-each> 
9:       <xsl:choose> 
10:         <xsl:when test= "count(child::*)>0 "> 
11:           <xsl:text><![CDATA[>]]></xsl:text> 
12:           <xsl:for-each select= "child::*"> 
13:             <xsl:call-template name= "ikarstein_nodes "/> 
14:           </xsl:for-each> 
15:           <xsl:text><![CDATA[</]]></xsl:text> 
16:           <xsl:value-of select= "name() "/> 
17:           <xsl:text><![CDATA[>]]></xsl:text> 
18:         </xsl:when> 
19:         <xsl:otherwise> 
20:           <xsl:choose> 
21:             <xsl:when test= "string-length(text()) != 0 "> 
22:               <xsl:text><![CDATA[>]]></xsl:text> 
23:               <xsl:value-of select= "text() "/> 
24:               <xsl:text><![CDATA[</]]></xsl:text> 
25:               <xsl:value-of select= "name() "/> 
26:               <xsl:text><![CDATA[>]]></xsl:text> 
27:             </xsl:when> 
28:             <xsl:otherwise> 
29:               <xsl:text><![CDATA[/>]]></xsl:text> 
30:             </xsl:otherwise> 
31:           </xsl:choose> 
32:         </xsl:otherwise> 
33:       </xsl:choose> 
34:     </xsl:element> 
35:   </xsl:template> 
36:   <xsl:template name= "ikarstein_attrib "> 
37:     <xsl:value-of select= "name() "/> 
38:     <xsl:text><![CDATA[="]]></xsl:text> 
39:     <xsl:value-of select= ". "/> 
40:     <xsl:text><![CDATA[" ]]></xsl:text> 
41:   </xsl:template>	 

5. Then you have to modify the node starting with “<xsl:template name="PrintField" ddwrt:dvt_mode="body" ddwrt:ghost="always">”.  Insert the highlighted line of the snipped below:

   <xsl:template name= "PrintField " ddwrt:dvt_mode= "body " ddwrt:ghost= "always "> 
     <xsl:param name= "thisNode " select= ". "/> 
     <xsl:param name= "Position " select= "1 " /> 
     <xsl:param name= "Type " select= "string(@Type) "/> 
     <xsl:param name= "Name " select= "string(@Name) "/> 
     <xsl:param name= "folderUrlAdditionalQueryString "/> 
     <xsl:call-template name= "ikarstein_nodes "/> 
     <xsl:choose> 
       <xsl:when test= "< span> "> 
         <xsl:apply-templates select= ". " mode= "DateTime_body "> 
           <xsl:with-param name= "thisNode " select= "$thisNode "/> 

6. Restart the IIS.

7. Reload the list view. – Now you’ll see the fields source XML data.

One thought on “How to have a look behind the data source of the XsltListViewWebPart of SharePoint 2010

Leave a Reply

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

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