Wednesday 13 June 2012

DateTimeField custom date format

Before you get your hopes all up there is no straightforward way to do this; though three possible workarounds come to mind:
  • use jQuery on page load to format your date however you please
  • add a codebehind to your pagelayout/masterpage
  • Utilize a calculated column
I proceed with the third option:
First create your site columns

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <Field ID ="{FD53DB35-0C02-45D8-9737-7C32A4184B86}"
         Group ="Portals Columns"
         Name ="Cust_EffectiveDate"
         DisplayName ="Effective Date"
         Type ="DateTime"
         Format="DateOnly" />
  <Field ID ="{8E90C71A-C8C1-4CD9-A0BC-37482F03EF8E}"
         Name ="Cust_EffectiveDate_Display"
         DisplayName="Published Effective Date"
         Group="Portals Columns"
    <Formula>=TEXT([Effective Date],"MMMM dd, yyyy")</Formula>

With your site columns deployed the next step is to make your content type, make sure to inherit from page

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900a689672336e64278beb708d5d4807b7f"
               Group="CCW Portals Content Types"
               Description="Custom content type for Policy or Procedure pages"
      <FieldRef ID ="{FD53DB35-0C02-45D8-9737-7C32A4184B86}" Name ="cust_EffectiveDate" />
      <FieldRef ID ="{8E90C71A-C8C1-4CD9-A0BC-37482F03EF8E}" Name ="cust_EffectiveDate_Display"/>
Now that your Custom content type is created go ahead and create a custom page layout that inherits from your content type.

Then in your page layout to display the DateTimeField in edit mode and the CalculatedField in display mode.

<PublishingWebControls:EditModePanel ID="DateEditModePanel" runat="server">
    <SharePoint:DateTimeField ID="EffectiveDateField" FieldName="Cust_EffectiveDate" runat="server" />
<PublishingWebControls:EditModePanel ID="DatePublishModePanel" PageDisplayMode="Display" runat="server">
    <SharePoint:CalculatedField ID="CalculatedDateField" FieldName="Cust_PublishedEffectiveDate_Display" runat="server" />