Tuesday, June 26, 2007

Free SharePoint DataView Tips

 


Free SharePoint DataView Tips


Hey Everyone... I know I've been out of touch but I wanted to post some DataView tips that I think people will find very useful.  I hope you do as well :o)  Do you have one you'd like to share that you can't live without?  Drop me a line and I'll add it to this list giving full credit.


 


Tip 1: Most people do not realize that when you create a DataView within FrontPage that you are actually connecting to the List's default view of the data.  This is a frustrating exercise to try and figure out why a newly created column doesnt display in your dataview regardless of how many times you refresh your content.  Save yourself a lot of time and frustration and make sure the columns are displayed in your default views.


 


Tip 2: If you're working with DataViews, you've probably run into a lot of frustration when trying to put conditional formatting on dates (i.e. only display items in the past or future using [Today]).  Well most programmers will hit the code and add scripts and modify the XSLT all over the place.  Your FrontPage experts will have you do numerous clicks to format the data and then do your comparisons.  Well, the easiest way to do this conditional formatting is to do it in the list itself.  As mentioned above, DataViews use the default view of your list.  Just modify columns and settings on your list and apply the quick and easy filter to the default view.  Once you create your DataView, you'll pick up the default views with conditional formatting already done.  There's a lot less overhead to applying the filter to the data since it is already done making your page load much quicker without the XSLT overhead ;o)


 


Tip 3: Have you had tried groupings in your DataView?  You'll notice right away that you lose some basic functionality of displaying the number of items in a grouping.  Here's some code to get you through that. 


The following returned the total number of items in the list:


<xsl:otherwise><xsl:value-of select="$fieldvalue"/> : <xsl:value-of select="count($nodeset)"/></xsl:otherwise> 

 


The following returned the number of list items in the grouping (Default functionality in non-dataview List Views):


<xsl:value-of select="$fieldvalue"/> : <xsl:value-of select="count($nodeset[*[name()=$fieldname]=$fieldvalue or ($fieldtype!='isodatetime' and @*[name()=$fieldname]=$fieldvalue) or ($fieldtype='isodatetime' and ddwrt:GenDisplayName(string(@*[name()=$fieldname]))=$fieldvalue) or (not(*[name()=$fieldname] or @*[name()=$fieldname]) and $fieldvalue = ' ')]/@ID)"/> 

 


 


Tip 4: Do you want your DataView to display a document type icon and have it render based on document type?  Try this within your XSLT:


<!--DocIcon--><!--Type--><xsl:choose><xsl:when test="@FSObjType='1'"> <xsl:choose> <xsl:when test="ddwrt:GetVar('RecursiveView')='1'"> <IMG BORDER="0" ALT="Icon" src="http://www.sharepointblogs.com/_layouts/images/folder.gif" mce_src="http://www.sharepointblogs.com/_layouts/images/folder.gif"/> </xsl:when> <xsl:otherwise> <A TABINDEX="-1" href="{@FileRef}BLOCKED SCRIPTSubmitFormPost()" mce_href="{@FileRef}BLOCKED SCRIPTSubmitFormPost()" onclick="BLOCKED SCRIPTClearSearchTerm('{$View}');BLOCKED SCRIPTSubmitFormPost('{$FilterLink}');BLOCKED SCRIPTreturn false;"> <IMG BORDER="0" ALT="Icon" src="http://www.sharepointblogs.com/_layouts/images/folder.gif" mce_src="http://www.sharepointblogs.com/_layouts/images/folder.gif"/> </A> </xsl:otherwise> </xsl:choose> </xsl:when>

 


Tip 5: Do you want to add a “New!” icon to your DataView?  Try this in your XSLT:


<xsl:if test="not(number(translate(substring-before(@Created,'T'),'-','')) < number(translate(substring-before($Today,'T'),'-','')))"><img border="0" src="http://www.sharepointblogs.com/_layouts/1033/images/new.gif" mce_src="http://www.sharepointblogs.com/_layouts/1033/images/new.gif"/></xsl:if>

 


Tip 6: If you've been working with DataViews you'll notice that Lookup fields do not translate well.  They are formatting with the title and the value of the data and are separated by special characters.  Here's how you can deal with this (Thanks to the Dean for refreshing my memory a few months back):


<xsl-value-of select="substring-after(@YourFieldName, '#')"> 

 


Tip 7: This tip comes from Pankaj Joshi who was working with a Multi-Choice checkbox field titled "Sub Network".  For the line:


<xsl:value-of select="@Sub_x002d_Network"/>

The output for two selected sub networks returned is:


;#Network A;#Network B;#

If you change the xslt to:



<xsl:value-of select="substring(substring(translate(translate(@Sub_x002d_Network, ';', ','), '#', ' '),2),1,string-length(substring(translate(translate(@Sub_x002d_Network, ';', ','), '#', ' '),2))-2)"/> 


Your new output will look like


Sub-network A, Sub-network B


 


Tip 8: Another Multi-Choice checkbox field alternative to formatting... let's say you have a choice field with three options selected.  When you go to data view, you see something like ";#Option1;#Option2;#Option3;#"


The XSLT will look like this:


<xsl:value-of disable-output-escaping="no" select="@FIELDNAME"/>

Change the line to:



<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="substring-after(@FIELDNAME,';#')"/>
<xsl:with-param name="from" select="';#'"/>
<xsl:with-param name="to" select="'; '"/>
</xsl:call-template>


Include this template if it does not already exist:




<xsl:template name="stringreplace">
<xsl:param name="stringvalue"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($stringvalue, $from)">
<xsl:value-of select="substring-before($stringvalue, $from)"/>
<xsl:if test="contains(substring($stringvalue, 1, string-length($stringvalue) - 1), $from)">
<xsl:value-of select="$to"/>
<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="substring-after($stringvalue, $from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$stringvalue"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Note: The rendered view in FrontPage will chop the first value off.  For some reason (a bug?) the interface places a semicolon ";" right before the first value when rendered over the web but FrontPage does not render that first semicolon.  To see this in action replace the substring-after function with:


<xsl:with-param name="stringvalue" select="@FIELDNAME"/>


Good luck!