Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 232 posts 902 karma points c-trib
    May 14, 2021 @ 16:51
    Martin Rud
    0

    The name 'ContentSavedState' does not exist in the current context

    Hi,

    I have this code to set a default value in properties named 'date' (inspired from https://our.umbraco.com/documentation/reference/events/EditorModel-Events/), but gets the above error.

    How can I fix it?

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Web.Editors;
    
    namespace My.Website
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class SubscribeToEditorModelEventsComposer : ComponentComposer<SubscribeToEditorModelEvents>
        {
        //this automatically adds the component to the Components collection of the Umbraco composition
        }
    
        public class SubscribeToEditorModelEvents : IComponent
        {
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
            }
    
            // terminate: runs once when Umbraco stops
            public void Terminate()
            {
                //unsubscribe during shutdown
                EditorModelEventManager.SendingContentModel -= EditorModelEventManager_SendingContentModel;
            }
    
            private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
            {
    
                foreach (var variant in e.Model.Variants)
                {
                    if (variant.State == ContentSavedState.NotCreated)
                    {
                        var pubDateProperty = variant.Tabs.SelectMany(f => f.Properties).FirstOrDefault(f => f.Alias.InvariantEquals("date"));
                        if (pubDateProperty!=null){
                            pubDateProperty.Value = DateTime.UtcNow;
                        }
                    }
                }
    
            }
        }
    }
    
  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    May 15, 2021 @ 09:40
    Marc Goodson
    100

    Hi Martin

    hmm, is it missing a using statement at the top?

    eg

    using Umbraco.Web.Models.ContentEditing;
    

    I have a feeling that's where the Enum for ContentSavedState lives

    if so we should update the docs example too!

    regards

    Marc

  • Martin Rud 232 posts 902 karma points c-trib
    May 15, 2021 @ 11:39
    Martin Rud
    0

    Thanks! :)

    That did the job. Should I do anything to get the docs example updated?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    May 15, 2021 @ 12:14
    Marc Goodson
    1

    Hi Martin

    Yes, you can, the documentation is all open source too.

    So if you visit the page in the docs, you'll see in the top right hand corner a button that says 'Edit This Page'

    enter image description here

    That'll take you to github, where you can add in the extra using statement to the example, and then when you save, give you the option to open a pull request into the docs, a member of the documentation curators team will review the request, and excitingly, once it's merged in - you'll get a shiny [C-Trib] contributor badge to your Our Umbraco profile, and be the envy of your Umbraco developing breathren... but more importantly someone else in the future won't get stuck on the same issue, and will have more time to create something cool, that hopefully they'll open source for us to use!

    regards

    marc

  • Martin Rud 232 posts 902 karma points c-trib
    May 15, 2021 @ 14:15
    Martin Rud
    1

    Done. :)

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    May 18, 2021 @ 19:10
    Marc Goodson
    0

    cool thanks, nice shiny C-Trib badge I see :-P

  • Martin Rud 232 posts 902 karma points c-trib
    May 18, 2021 @ 20:45
    Martin Rud
    0

    Yes! :)

Please Sign in or register to post replies

Write your reply to:

Draft