November Happy Hour will be moved to Thursday December 5th.

How to change a page's PageChanged date?

Vote:
 

Hi!

Is there any way I can override a page's PageChanged programmatically? Not sure if Im even on the right track on this.
I tried to use this blog. https://world.episerver.com/blogs/Kalle-Ljung/Moving-built-in-properties-to-the-settings-header-in-EPiServer-7-CMS/ so that I could make the PageChanged property writeable by using the following code:

public class SiteMetadataExtender : IMetadataExtender
    {
        public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable attributes)
        {
            foreach (var modelMetadata in metadata.Properties)
            {
                var property = (ExtendedMetadata) modelMetadata;
                if (property?.PropertyName != "ichangetrackable_changed") continue;
                property.IsReadOnly = false;
                //Try to make this writeable. Not sure if Im even remotely on the right course
            }
        }
    }

And this:

[InitializableModule]
    [ModuleDependency(typeof(InitializableModule))]
    public class SiteMetadataExtenderInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            if (context.HostType != HostType.WebApplication) return;
            var registry = context.Locate.Advanced.GetInstance();
            registry.RegisterMetadataHandler(typeof(ContentData), new SiteMetadataExtender());
        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context) { }
    }

Then trying to change PageChanged here:

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule),
        typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var events = context.Locate.ContentEvents();
            events.PublishedContent += UpdatePageChanged;
        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }


        public void UpdatePageChanged(object sender, ContentEventArgs e)
        {
            var sitePageData = e.Content as SitePageData;
            
            if (sitePageData != null)
            {
                var clone = sitePageData.CreateWritableClone();
                clone.SetValue(MetaDataProperties.PageChanged, new DateTime(2016, 5, 5, 5, 5, 5));

                var repo = ServiceLocator.Current.GetInstance();
                repo.Save(clone, AccessLevel.Publish);
            }
        }
    }






#180283
Jul 05, 2017 10:59
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.