Copied to clipboard

Flag this post as spam?

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


  • Sean Håkansson 66 posts 192 karma points
    Aug 24, 2015 @ 00:57
    Sean Håkansson
    0

    Creating page with Content Service with custom CreateDate not working?

    Hi!

    I'm currently building a social feed import which imports instagram data and media.
    Everything works fine for custom properties, but I always get the import date instead of my custom date.

    page.SetValue("o_created", item.Created);
    page.CreateDate = item.Created;
    contentService.SaveAndPublishWithStatus(page);
    

    o_created gets the right date.
    CreateDate gets the date of the import to Umbraco.

    Should I use SetValue for CreateDate instead, or I'm doing something wrong here? Maybe I should use publish at instead?

    Thanks for any help!

  • Robert Foster 459 posts 1820 karma points MVP 2x admin c-trib
    Aug 24, 2015 @ 02:08
    Robert Foster
    0

    Hi Sean, CreateDate and UpdateDate are system fields and shouldn't be assigned to (CreateDate is internally set to the current Date/Time when the content item is created) - treat it as internal use only, and use your own custom property for anything to do with sorting/filtering/viewing the imported create date instead.

    Publish At (ReleaseDate) is used to specify a date/time that you want a Content item to be published using Umbracos' built in task scheduler, so that's not going to help you at all.

  • Sean Håkansson 66 posts 192 karma points
    Aug 24, 2015 @ 06:26
    Sean Håkansson
    0

    Thanks for your reply!

    If I was to write a script to migrate a news section I would always need to write to a custom property? Or do people usually write directly to database for migration? Why shouldn't system fields be writable, at least by developers, performance or stability issues?

    ...

    A bit off topic, how would I write a sort by with a fallback? I've tried google, but my lingo is probably off because I can't get any matches.

    Code for sorting:

    children.OrderBy("o_created descending, CreateDate descending")
    

    Only seems to order them by group. Results in the following order:

    Tweet - 2015-07-21
    Tweet - 2015-07-19
    Project 1 - 2015-08-22
    Project 2 - 2015-08-20
    

    But I would like it to be:

    Project 1 - 2015-08-22
    Tweet - 2015-07-21
    Project 2 - 2015-08-20
    Tweet - 2015-07-19
    

    Thanks!

  • Robert Foster 459 posts 1820 karma points MVP 2x admin c-trib
    Aug 24, 2015 @ 06:54
    Robert Foster
    0

    Never write to the database - lots of potential problems ensue :)

    Generally, the only field you shouldn't need to add is the Name (and many people add an alternative PageTitle or something similar anyway).

    If you're dealing with pre-existing data that includes things like create date or anything else, you'll need a custom property.

    My standard Article Document Type for example always includes a "Post Date" property so I can set the date I want the post to appear to have been created on specifically.

    As for ordering, the default order is the order in which the Document is created - so if you sort your import data by your desired property prior to importing, that should handle it for you.

    For sorting on the fly once you've imported, just sort on your preferred field only, forget the CreateDate...

    Hope this helps...

    Rob.

  • Sean Håkansson 66 posts 192 karma points
    Aug 24, 2015 @ 07:46
    Sean Håkansson
    0

    So your require your editors to set the Post Date for new articles as well?

    I'm with you on leaving CreateDate alone , but I want to use it as a fallback for when there is no need for the editor to enter current date. But I can't just get the SortBy right as described above.

    Thanks
    Sean

  • Robert Foster 459 posts 1820 karma points MVP 2x admin c-trib
    Aug 24, 2015 @ 07:58
    Robert Foster
    0

    With the Post Date I automatically set it by hooking into the Save/Publish events - however, I have it set up so that the Editor has the option of changing it:

    https://our.umbraco.org/documentation/Reference/Events/application-startup

    My code for that might look like this for example:

    public class ArticleEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            global::Umbraco.Core.Services.ContentService.Saving += ContentService_Saving;
        }
    
        private void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<IContent> e)
        {
            foreach (var a in e.SavedEntities.Where(a => a.ContentType.Alias == "Article"))
            {
               if (a.GetValue("postDate") == null || a.GetValue<DateTime>("postDate") == DateTime.MinValue)
                   a.SetValue("postDate", a.CreateDate == DateTime.MinValue ? DateTime.Now : a.CreateDate);
            }
        }
    }
    

    Anyway, back to the SortBy...

    My preference is to use standard LINQ wherever possible, so I don't normally use Dynamics for this kind of stuff (or do it on a Controller/Helper class so I don't have to expose the business logic in the View) - from what you're doing above (using OrderBy("")) it looks like you're using dynamics, but I could be wrong.

    Could you please post a bit more of a code snippet so we have some context on the children variable? And are you doing this in the View, or a compiled dll somewhere?

  • Sean Håkansson 66 posts 192 karma points
    Aug 24, 2015 @ 08:57
    Sean Håkansson
    0

    Nice, thanks for the example code and all your help!

    You're right, I was using dynamics.
    I switched to typed instead of dynamic so I could use LINQ/Lambda, don't know if possible with dynamics - I gave up finding something useful.

    Anyway this is LINQ code for date fallback:

    OrderByDescending(c => c.GetProperty("o_created").HasValue ? (DateTime)c.GetProperty("o_created").Value : c.CreateDate))
    

    Btw my snippet to fetch children (this was done in a partial view):

    var pageRoot = CurrentPage.AncestorsOrSelf(1).FirstOrDefault();
    var umbracoPages = pageRoot.DescendantsOrSelf(2)
    

    Million thanks!

Please Sign in or register to post replies

Write your reply to:

Draft