Group items in a data view with the ability to nest items

August 25th, 2008 | Categories: SharePoint

SharePoint Designer’s typical Sort&Group is good for basic needs. But if you need to do some nesting (like nested bulleted lists of groupped items) the standard (GUI) way with SharePoint Designer just won’t do, since all the elements (even the members of a group) are not nested. Let me explain a bit more in detail what I have in mind. Let’s have a simple list with fields Full Name and Company. (both single line of text, or you can have the "Company" as lookup. Doesn’t matter at this stage.

image

So we want to create a list like follows:

  • B4Contact
    • Boris Gomiunik
    • Jose Morales
  • Company1
    • John Doe
    • John Dooe
  • Company2
    • Jane Doe

The example above ofcourse is done by having the nested bulleted lists (

    ). To achieve this let’s open the site in SharePoint Designer and insert the list’s Data View into the page. (I usually do this by dragging the list from Data Source Library to the page.

    image

    Now let’s change the layout to something very simple – a bulleted list.

     image

    image

    Before we dig in, we need to sort according to the field we want it groupped by.

    image

    Nothing fancy untill this point. Now it’s time to switch to the Code or Split View.

    We want the first level to display company name, so let’s locate the

    (I’ve renamed the Title Field to Full Name. In case you’re replacing any other field, locate it and then change it to

    (or whichever field you’ll want displayed in the first level).

    image

    Let’s do some grouping now. Next in your code locate the following part

    
      
       

    And change the first line of that part to:

    Note the filter in square brackets that is added. Ofcourse replace @Company for the SharePoint field name you’re grouping with. This will result in a groupped first level – Companies.

    image

    Now let’s start building the nested subview. First let’s move the

  • element from template dvt_1.rowview to dvt_1.body to avoid too many xsl:templates and add a nested
      . In the code below the first sample is the original and the other is changed.

      
        
         
         
             
         



         

    •        
         
                 

             

    • 
        
         
         
             

    •        

               
               

    •    



      Now that we’ve cleared the xsl template dvt_1.rowview we can use it for the nested list of employees. But first we need to pass some parameters to that template – more specifically – all the rows and company name for filtering. So replace the line

      with

      
        
         
         

      Now let’s edit the dvt_1.rowview template. Locate the

      and add the following below:

      
        

      After those two add the following:

      
        
         

    • and you’re done. Now you have a nested grouped data view

      image

      To sum up the whole XSL code is:

      
      
      	
      	
      	'
      	0
      	
      		
      	
      	
      		BulTitl
      		
      		
    • In the code above pay special attention to the part from section.

      This way you can nest lists, layers or any elements you want.