Copied to clipboard

Flag this post as spam?

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


  • Louis Ferreira 69 posts 265 karma points
    Mar 07, 2022 @ 13:24
    Louis Ferreira
    0

    ContentService.GetValue<DateTime>() not returning saved value.

    Hello,

    Umbraco version 8.9.1

    I'm creating a scheduled task component that needs to 'Move' a node based on a DateTime property on the page set by the editor. i.e. a sort of 'move to archive folder after this date & time' thing.

    On the node, I have this property... enter image description here

    with the date and time set. It displays this value correctly if I render it to a razor page.

    However, in the component, when I try get the value for this property using the ContentService, it returns the default DateTime value as follows... enter image description here

    Here is the block of code in my component that is used to retrieve the content...

    public override bool PerformRun()
        {
            long totalRecords = 0;
            var newsAndEventsDocType = _contentTypeService.Get("newsAndEvents");
            var newsAndEventPages = _contentService.GetPagedOfType(newsAndEventsDocType.Id, 0, 999, out totalRecords, null);
            if (totalRecords == 0)
                return true;
    
            totalRecords = 0;
    
            foreach (var newsAndEventPage in newsAndEventPages)
            {
                var articles = _contentService.GetPagedChildren(newsAndEventPage.Id, 0, 999, out totalRecords);
    
                if (totalRecords > 0)
                {
                    foreach (var item in articles)
                    {
                        if (item.HasProperty("archiveDate")) //returns true 
                        {
                            var page = _contentService.GetById(item.Id);
                            var dateValueContent = page.GetValue<DateTime>("archiveDate"); // always returns 0001/01/01 00:00 !!
                            if (dateValueContent > DateTime.MinValue && dateValueContent <= DateTime.Now)
                            {
                                _logger.Info<ArchiveArticles>("This article is about to be moved to Archive: {0}", item.Name);
    
                            }
    
                        }
                    }
                }
    
            }
    
            return true;
        }
    

    I checked other properties with DateTime types on the same page, and they too return default DateTime.Min value. Can anyone confirm that this is the correct way of doing this, or is there another way that works?

    Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft