Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steven Wilber 103 posts 98 karma points
    Feb 19, 2010 @ 12:32
    Steven Wilber
    0

    How to change a Documents Create Date

    I found this post on the old forum:

    http://forum.umbraco.org/yaf_postst6303_Change-Documents-Created-Date.aspx

    An can't for the life of me find it in the new forum (any advice on how to do this?), but I wanted to post an answer to the question, or at least my take on it.

    My way to solve the problem of importing old content is to include a field in the document type called 'datePublished'. This is the set, then in an event I check for this and override the Created Date and clear out the 'datePublished'.

    I have an Events class which inherits from ApplicationBase and in the constructor (extract shown below) establish the events (remember to always allow them to the turned off) (apologies for the VB.NET - client requirement)

            settingValue = ConfigurationManager.AppSettings.Get("EnableUpdateCreatedDateEvent")
            If Not String.IsNullOrEmpty(settingValue) AndAlso settingValue.ToLower() = "true" Then
                AddHandler Document.BeforeSave, AddressOf Me.UpdateCreatedDate
            End If

    The code for updating the Create Date is easy too.

        Private Sub UpdateCreatedDate(ByVal sender As Document, ByVal e As SaveEventArgs)
            'if the document has a property called 'datePublished' then if it has a value and it is a date, then we'll use it to override the main date and then clear the field

            Dim PROPERTY_NAME As String = "datePublished"
            Dim datePublishedValue As String = Ed.Helper.Umbraco.GetDocumentPropertyValue(sender, PROPERTY_NAME)
            Dim datePublished As DateTime

            If Not String.IsNullOrEmpty(datePublishedValue) AndAlso DateTime.TryParse(datePublishedValue, datePublished) Then
                sender.CreateDateTime = datePublished
                ED.Helper.Umbraco.DeleteDocumentProperty(sender, PROPERTY_NAME)
            End If

        End Sub

    The "ED.Helper" is just a helper class that I created and use extensively to hide away the exceptions that Umbraco raises. In normal Umbraco you can delete a property by calling its 'delete()' method and you can access it's value using code something like the following:

                string retval = null;

                if (content != null)
                {
                    Property property = null;
                    try
                    {
                        property = content.getProperty(propertyName);
                    }
                    catch { }
                    if (property != null)
                    {
                        retval = property.Value.ToString();
                    }
                }

    // return the result
    return retval;

     

    Hopfully that helps someone, if not me in the future. Happy to answer any questions.

    Cheers

    Steve

  • Steven Wilber 103 posts 98 karma points
    Feb 19, 2010 @ 13:04
    Steven Wilber
    0

    As a quick follow on - updating properties in events means that they don't show the updated values once the page refreshes. This is because the events are triggered during the page load and there is no opportunity to force the display of new values.

    The only way round this (if it is a problem) that I have found is this solution by the excellent Sebastiaan Janssen.

    Cheers

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft