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;
}
}
}
}
}
}
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'
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!
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?
Hi Martin
hmm, is it missing a using statement at the top?
eg
I have a feeling that's where the Enum for ContentSavedState lives
if so we should update the docs example too!
regards
Marc
Thanks! :)
That did the job. Should I do anything to get the docs example updated?
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'
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
Done. :)
cool thanks, nice shiny C-Trib badge I see :-P
Yes! :)
is working on a reply...