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="http://schemas.microsoft.com/sharepoint/">
  <Field ID ="{FD53DB35-0C02-45D8-9737-7C32A4184B86}"
         Group ="Portals Columns"
         Name ="Cust_EffectiveDate"
         DisplayName ="Effective Date"
         Type ="DateTime"
         Required="FALSE"
         Format="DateOnly" />
  <Field ID ="{8E90C71A-C8C1-4CD9-A0BC-37482F03EF8E}"
         Name ="Cust_EffectiveDate_Display"
         DisplayName="Published Effective Date"
         Group="Portals Columns"
         Type="Calculated"
         ResultType="Text" 
         ReadOnly="TRUE" 
         Required="FALSE">
    <Formula>=TEXT([Effective Date],"MMMM dd, yyyy")</Formula>
  </Field>
</Elements>

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="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900a689672336e64278beb708d5d4807b7f"
               Name="PolicyProcedureType"
               Group="CCW Portals Content Types"
               Description="Custom content type for Policy or Procedure pages"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID ="{FD53DB35-0C02-45D8-9737-7C32A4184B86}" Name ="cust_EffectiveDate" />
      <FieldRef ID ="{8E90C71A-C8C1-4CD9-A0BC-37482F03EF8E}" Name ="cust_EffectiveDate_Display"/>
    </FieldRefs>
  </ContentType>
</Elements> 
 
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>
 
<PublishingWebControls:EditModePanel ID="DatePublishModePanel" PageDisplayMode="Display" runat="server">
    <SharePoint:CalculatedField ID="CalculatedDateField" FieldName="Cust_PublishedEffectiveDate_Display" runat="server" />
</PublishingWebControls:EditModePanel>