Friday, July 29, 2011

SharePoint Data View Trick

When making a new data view certain fields are too long to fit in the cell/div that you need it too. So here’s some XSLT I’ve been using to truncate a field.



                <a href="@FileRef”> 
<xsl:variable name="truncate-length” select="40”/>
<xsl:value-of select="substring(@Title,1, $truncate-length)”/>
<xsl:if test="string-length(@Title) > $truncate-length”>
<xsl:text>...xsl:text>
xsl:if>
a>

The above example runs inline, and grabs the URL and Title fields from a list item. It then truncates the Title to the length specified in the truncate-length variable to however many characters you want to use and then appends on ... to the end. Fairly straightforward but can be hard for some to build from scratch.

Next, how to customize the built in [Current Date] field. Every time you make a filter off of [Current Date] an entry for it gets added in the code behind for the data view under the tag “SharePoint:SPDataSource”. The easiest way I’ve found to edit this field is to export it to notepad, or any basic text editor and replace the masked characters in it with the right ones

& lt; = < 
& gt; = >
& quot; = "

That will make it much easier to read

<View>     <Query>         <Where>             <And>                 <Geq>                     <FieldRef Name="EventDate”/>                     <Value Type="Text”>                         <Today/>                     Value>                 Geq>                 <Leq>                     <FieldRef Name="EndDate”/>                     <Value Type="Text”>                         <Today OffsetDays="14”/>                     Value>                 Leq>             And>         Where>         <OrderBy>             <FieldRef Name="EventDate” Ascending="TRUE”/>         OrderBy>     Query> View>

Unfortunately you can’t paste this much more readable code back in to SharePoint designer, but you can at least find where you need to edit it. In this case were looking for the Today tag after EndDate. I added the OffsetDays attribute and gave it a length of 14 days. You can also go negative for this value, so you can see things from previous times rather than future ones.


No comments: