The LandPhil be honest, be honorable, be kind, be compassionate, and work hard.

August 9, 2011

SharePoint Designer: Getting the DateTime into a readable format

Filed under: SharePoint — phil @ 10:15 am
There are 3 view forms in SharePoint Designer for lists:
New
Edit
View
The default versions of these forms are created by SharePoint when the list is created.  They use a ListFormWebPart to display their information.  This is done by querying SharePoint for the objects directly.
When you create a custom form of any of the types specified above (New, Edit, or View), the default web part that is included is actually the DataFormWebPart.  This queries SharePoint for data and feeds it into an XML stream and the web part uses XSLT to parse data out of the string.  Unfortunately, this means that all parts of the SharePoint objects are serialized and thus you may run into problems rendering the data in string form.
Case in point, the DateTime object.  The DateTime object has fields for the Date and the Time separately.  When serialized, however, the data is concatonated into a single string.  This has two downsides, 1) A single string with no white space is hard to read, and 2) since the time is stored in zulu time (so that a particular user can display based on his chosen time zone), the string data is also displayed in zulu time.
There is, fortunately, a way around this.  If you edit the DataFormWebPart and do the following:
1.  Right above the table that contains the data rows, below the <xsl:template> tag, insert:
  • <xsl:param name=”Pos” select=”position()” />
2.  In the table row that contains the dates and times you wish to display:
  • Comment out the <xsl:value-of select=”*” />  with <!– –>
  • Add the following (inside of the <td> tags):
  • <SharePoint:FormField runat=”server”id=”ff3{$Pos}” ControlMode=”Display”FieldName=”EventDate”__designer:bind=”{ddwrt:DataBind(‘i’,concat(‘ff3′,$Pos),’Value’,’ValueChanged’,’ID’,ddwrt:EscapeDelims(string(@ID)),’@EventDate’)}”/>
  • <SharePoint:FieldDescription runat=”server” id=”ff3description{$Pos}” FieldName=”EventDate” ControlMode=”New”/>
3.  Replace parts of the code as follows:
  • id=ff#{$Pos} (where # is equal to the display row on the form)
  • ControlMode=”Display” (can be Edit, New, or Display)
  • FieldName=” “ (Use the same field name as the value-of select statement you commented out)
  • bind=” “ (in the bind string change the row position number and the field name)
  • Update the id, FieldName and ControlMode settings of the SharePoint:FieldDescription to match SharePoint:FormField
Viola, the date and time in a readable format and still encoded with the logged in user’s time zone choice.

2 Comments »

  1. There’s a much simpler way of doing this, as I have since learned. If you create a new “Display” form, all of the list fields are referenced with an ‘xsl:value-of select’ statement. However, if you create a new “Edit” or “New” form, all of the list fields are referenced by their corresponding ‘SharePoint:FormField’ and ‘SharePoint:FieldDescription’ tags. You can add the ‘xsl:param’ at the top of the template and then copy/paste the fields from a new or edit form and change the ControlMode to “Display”.

    Comment by phil — November 13, 2012 @ 3:46 pm

  2. The caveat here is that any columns that you add AFTER you create your custom forms won’t display. My advice there is to create a new custom form of the appropriate type long enough to copy/paste the new field into the old custom form.

    Comment by phil — November 13, 2012 @ 3:48 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress