Copied to clipboard

Flag this post as spam?

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


  • Vincent Baaij 95 posts 344 karma points c-trib
    Jan 29, 2013 @ 22:52
    Vincent Baaij
    0

    V6: How to subscribe to ContentService events?

    Morten,

    As we discussed on Twitter, I could use some help J I’m trying to implement your Standard Values package in a V6 Umbraco site I’m building.

    I now have the following code (I renamed StandardValues to DefaultValues):

     

    namespace xxx.DefaultValues.Events
    {
        public class LoadDefaultValueEvent : IApplicationEventHandler
        {
            public LoadDefaultValueEvent()
            {
                Document.New += Document_New;
            }
            private void Document_New(Document sender, NewEventArgs e)
            {
                ContentType contentType = sender.ContentType;
                DataAccess da = new DataAccess();
                if (!da.NodeExists(contentType.Id)) return;
                var contentItem = new ContentItem(da.GetNodeId(contentType.Id));
                DefaultValue.SetDocumentProperties(contentItem.GenericProperties, sender);
            }
            public void OnApplicationInitialized(UmbracoApplication httpApplication, Umbraco.Core.ApplicationContext applicationContext)
            {
                throw new System.NotImplementedException();
            }
            public void OnApplicationStarted(UmbracoApplication httpApplication, Umbraco.Core.ApplicationContext applicationContext)
            {
                throw new System.NotImplementedException();
            }
            public void OnApplicationStarting(UmbracoApplication httpApplication, Umbraco.Core.ApplicationContext applicationContext)
            {
            }
        }
    }

    The first two functions are leftovers from the old ApplicationStartupHandler. After implementing the new interface I now have the three other functions. I think I need to implement OnApplicationStarting. Correct? I also think that in that function I must subscribe to the created event. Haven’t got a clue how to code that. Hope you can help me with that part.

    In my adapted V4 version I also implemented a ‘parent’ solution. I took another approach than you did in your latest check-in. Here’s how I did it in (StandardValues.cs):


    //Loop through each property of the current document
    foreach (Property property in properties)
    {
    object tempProperty = property.Value;
    if (tempProperty.ToString().Contains("$name$"))
    {
    tempProperty = tempProperty.ToString().Replace("$name$", sender.Text);
    }
    if (tempProperty.ToString().Contains("$parent$"))
    {
    Document parent = new Document(sender.Parent.Id);
    tempProperty = parent.getProperty(property.PropertyType.Alias).Value;
    }
    //Loop through each key in the config xml
    //check if property contains key and replace with new value
    foreach (string key in keys)
    {
    if (tempProperty.ToString().Contains(key))
    {
    tempProperty = tempProperty.ToString().Replace(key, GetValue(configXml, umbracoXml, key));
    }
    }
    if (tempProperty != null && !string.IsNullOrEmpty(tempProperty.ToString()))
    sender.getProperty(property.PropertyType).Value = tempProperty;
    }

     

    A small benefit (at least I think so) this way is that you don’t need to define the fields for which you want to take the parent value in the config file. By the way: I rewrote the part where it gets the parent property to:

     

                     if (sender.ParentId != -1)
                     {
                            var parent = ApplicationContext.Current.Services.ContentService.GetById(sender.Parent.Id);
                            tempProperty = parent.Properties[property.PropertyType.Alias].Value;
                     }

     

    Is that the right way to do it, you think?

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Jan 30, 2013 @ 15:55
    Morten Christensen
    100

    Hi Vicent,

    Here is an example of subscribing to the "Saving" event in v6, which correspond to the BeforeSave event in v4.
    Please note that the sender is the ContentService, which is what you are subscribing to - and because you are in a before save-event you don't need to call Save, as that will be done when the event execution is finished.

    using Umbraco.Core.Services;
    using umbraco.interfaces;

    namespace Umbraco.Examples
    {
    public class EventExample : IApplicationStartupHandler
    {
    public EventExample()
    {
    //Initialize the event subscriber
    ContentService.Saving += ContentService_Saving;
    }

    private void ContentService_Saving(IContentService sender, Core.Events.SaveEventArgs<Core.Models.IContent> e)
    {
    //Loop through the content objects that are passed in from the event
    foreach (var content in e.SavedEntities)
    {
    //Set a value on a Property by its Alias
    content.SetValue("myAlias", "object value");

    //Loop through properties on the content object
    foreach (var property in content.Properties)
    {
    //Ensure that the property value is not null and that it contains the $name$ key
    if (property.Value != null && property.Value.ToString().Contains("$name$"))
    {
    //Do something like set the value
    property.Value = "something";
    }
    }
    }
    }
    }
    }

    I'm sorry for the lack of explaination. I'll have to come back to this tomorrow, but I hope the above example helps shed some light on how you can go about changing the value of a property in v6.

    - Morten

Please Sign in or register to post replies

Write your reply to:

Draft