tags:

views:

655

answers:

2

I am trying to modify the date format displayed in a custom template using the field. What I want to display is this:

This page was last modified on 29 Jan 2010 by Joel Spolsky

(where the user name links to their profile).

I found some good examples here on http://mindsharpblogs.com/aaron/archive/2008/02/08/4283.aspx which helped me get the custom template set up. And I looked through the class members on the MSDN documentation. But I cannot figure out how to modify the date format.

Is there any way to pass a date format string such as "d MMM yyyy" to the FieldValue to use for rendering?

This is my current code which works except the date format comes in as 29/01/2010 19:22 which isn't as user-friendly.

<SharePoint:CreatedModifiedInfo ControlMode="Display" runat="server">
        <CustomTemplate>
            This page was last modified on
            <SharePoint:FieldValue FieldName="Modified" runat="server" ControlMode="Display" DisableInputFieldLabel="true"/>
            by
            <SharePoint:FormField FieldName="Author" runat="server" ControlMode="Display" DisableInputFieldLabel="true" />
        </CustomTemplate>
</SharePoint:CreatedModifiedInfo>
A: 

I think you can call there directly item field value and format it. Try this code below and add import tag to header.

<%@ Import Namespace="Microsoft.SharePoint" %>
...
<SharePoint:CreatedModifiedInfo ControlMode="Display" runat="server"> 
    <CustomTemplate> 
        This page was last modified on 
        <%=SPContext.Current.ListItem["Modified"]==null?"":((DateTime)SPContext.Current.ListItem["Modified"]).ToString("d MMM yyyy")%>
        by 
        <SharePoint:FormField FieldName="Author" runat="server" ControlMode="Display" DisableInputFieldLabel="true" /> 
    </CustomTemplate> 
</SharePoint:CreatedModifiedInfo> 
EG
Thanks for the answer, but this is not working for me. When I try to paste this in SharePoint Designer gives me the squiggly line that it is not allowed and then if I try to run it I get the error:"Code blocks are not allowed in this file"So it seems like I cannot use inline code in the page layout file. Any other ideas?
Peter Jacoby
I thought you editing controltemplat and yes, in designer inline code is not allowed. I can suggest alternatives - write your own web control and include it to the page, override control template in controltemplates or format string with javascript in client side.
EG
A: 

I found a slightly different solution the problem. I'm not 100% happy with it, but it's pretty simple. This was based on a solution I read here: http://panvega.wordpress.com/2009/03/16/masterpagepagelayout-format-date-field/

You create a custom column on the library of type "Calculated" and format the date however you want. In my case that meant:

=TEXT(Modified,"d MMM yyyy")

Then in the page layout I just reference this field and the formatting is already done:

<SharePointWebControls:CalculatedField ID="CalculatedField" FieldName="Display Date" runat="server" />

It's not as elegant a solution as I was looking for mostly because it requires adding a custom column on every page library. But it requires very little code.

I'm still open to a better solution. It's seems strange to have to write a whole web control just to format a date, but it seems that might be the only other better alternative.

Peter Jacoby