Copied to clipboard

Flag this post as spam?

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


  • David F. Hill 122 posts 242 karma points
    Feb 16, 2013 @ 01:46
    David F. Hill
    0

    BeforePublish Event - unable to assign property value

    Hello Umbraco Colleagues,

    I'm using Umbraco version 6.0.0 (or trying to... this is a show-stopper if I cannot figure it out).

    Could someone *PLEASE* post an example of assigning property values using the BeforePublish Event.

    I need to assign some property values using the BeforePublish Event but this appears to be obsolete in Umbraco version 6.

    public class myBeforePublish : umbraco.businesslogic.ApplicationStartupHandler

    here's the way I used to do it (and it used to work):

    public myBeforePublish()
    {
        Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
    }

    and the values are not being assigned with this:

    void AssignValue(Document sender)
    {
      Document documentObject = sender;
      if (documentObject.ContentType.Alias == myAlias)
      {
        documentObject.getProperty(myPropertyAlias).Value = "example";
      }
    }

    The visual studio warning says this:

    'umbraco.businesslogic.ApplicationStartupHandler' is obsolete: 'This class is no longer used, implement IApplicationEventHandler instead'

    But I've had no success implementing IApplicationEventHandler so far.

    I'd rather not revert back to ver. 4.11.x but I may have to if I can't get it.

    If someone would kindly supply an example, I'm sure others are having (or will soon have) the same trouble as I am.

    Thanks,
    David Hill

     

  • Dallas 127 posts 398 karma points
    Feb 16, 2013 @ 03:57
    Dallas
    0

    David, one of the breaking changes with v6 is that you need to call Save explicitly when setting properties.

    "you cannot set a property's value without doing an explicit Save() any more. So: doc.getProperty("pageTitle").Value = "Hello World"; does not do a save to the database, you have to call doc.Save(); after that."

    http://umbraco.codeplex.com/releases/view/101178


  • Jeroen Breuer 4860 posts 12121 karma points MVP 3x admin c-trib
    Feb 16, 2013 @ 15:41
    Jeroen Breuer
    1

    Hello,

    You can only set properties in the before save event. More info here: http://issues.umbraco.org/issue/U4-1372http://issues.umbraco.org/issue/U4-1564

    As an alternative you can call the .Save(false) method again in the before publish event, so the values get saved and no events are called to prevent an infinite loop.

    Jeroen

  • David F. Hill 122 posts 242 karma points
    Feb 16, 2013 @ 18:57
    David F. Hill
    0

    1)
    Thanks, Dallas, but umbraco.cms.businesslogic.web.Document' is obsolete also. I'm told to use Umbraco.Core.Models.Content instead. I'm uncertain as to how to do what I need to do with the new content service.

    2)
    Thanks, Jeroen. I'm a bit more confused on the whole matter now.

    Is that a bug you're describing?

    The warning says: 'ApplicationStartupHandler' is obsolete: 'This class is no longer used, implement IApplicationEventHandler instead... but I haven't been able to get anywhere with that.

    I was hoping for a simple code example to show how to assign a value when the user clicks "save and publish". I haven't been able to glue together all the various bits of information enough to create actual working code.

    It's beginning to sound like I cannot use version 6 for my applications. Too bad if that's the case. I really wanted to make use of a lot of the new MVC features.

    David

  • Jeroen Breuer 4860 posts 12121 karma points MVP 3x admin c-trib
    Feb 16, 2013 @ 19:03
    Jeroen Breuer
    0

    No it's not a bug. You can still use the old API's, but in v6 they work a bit different. The easiest solution is do to the stuff you wanted to do in the before publish event in the before save event. Otherwise you'll need to call .Save() again in the before publish event. I have an example, but it's a bit harder because I wanted my code to be compatible with v4 and v6. Check the Media_AfterSave method here: http://damp.codeplex.com/SourceControl/changeset/view/2df2dc8aab7c#DigibizAdvanceMediaPicker/DAMP_ApplicationBase.cs. It's the aftersave method, but using the before publish probably works the same.

    Jeroen

  • David F. Hill 122 posts 242 karma points
    Feb 16, 2013 @ 21:49
    David F. Hill
    0

    Thanks, Jeroen. I guess I have more work to do to get it to do what I want.

    Seems like it got a tad more complicated that it needs to be. Plus, I have always been reluctant to make calls to obsolete code (e.g. ApplicationStartupHandler and Document) - preferring to do things in the new paradigm with the current code base.

    My goal with this post was to see an example of code using the new version 6 API to write property values when someone presses save+publish.

    If anyone could post some or point me to an example - that would be great.

    Cheers,
    David

  • David F. Hill 122 posts 242 karma points
    Feb 17, 2013 @ 22:44
    David F. Hill
    0

    Update:

    For those who may need some sample code to write property values when the save+publish is pressed, here is an example using the ContentService.Saving event handler:

    using System;
    using Umbraco.Core.Services; //assembly: Umbraco.Core.dll
    using umbraco.interfaces;

    namespace FoodMethods
    {
        public class CustomEventHandler : IApplicationStartupHandler
        {
            public CustomEventHandler()
            {
                ContentService.Saving += CustomSavingEventHandler;
            }

            private void CustomSavingEventHandler(IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (var item in e.SavedEntities)
                {
                    if (item.ContentType.Alias == "blogPost")
                    {
                        string existing = item.GetValue("test").ToString();
                        string newData = "save:" + DateTime.Now.ToString();
                        item.SetValue("test", existing + "\n" + newData);
                    }
                }
            }
        }
    }

     

    As far as I can tell, in V6 there is no longer any such thing as the Document.BeforePublish that we had in V4.

    This is unfortunate because, for my application, I don't want the action to occur when the content item is saved but only when published.

    Also, this event gets called twice when the save+publish button is clicked - not sure if this is how it is supposed to be - but it will not work for me.

    I don't know if I'm doing it incorrectly because I haven't been able to find any documentation on the subject.

    This is very disappointing - i really wanted to learn and use the new V6 API and not have to call obsolete code.

    David

     

  • Sebastiaan Janssen 4841 posts 14344 karma points MVP admin hq
    Feb 19, 2013 @ 22:15
    Sebastiaan Janssen
    1

    Check out PublishingStrategy.Publishing :-)

  • David F. Hill 122 posts 242 karma points
    Feb 20, 2013 @ 20:38
    David F. Hill
    5

    Thanks, Seb. That's it!

    Here is a bit of example code for anyone who may want it:

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

    namespace FoodMethods
    {
        public class CustomEventHandler : IApplicationStartupHandler
        {
            public CustomEventHandler()
            {
                Umbraco.Core.Publishing.PublishingStrategy.Publishing += CustomPublishingEventHandler;
            }

            private void CustomPublishingEventHandler(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (var item in e.PublishedEntities)
                {
                    if (item.ContentType.Alias == "test")
                    {
                        // this adds a message to the existing value of the (textstring multiple) property
                        string newData = "publishing: " + DateTime.Now.ToString();
                        item.SetValue("myProperty", item.GetValue("myProperty").ToString() + "\n" + newData);
                    }
                }
            }
        }
    }

     

    Note - we don't need to call the "save" method because the item is on its way to being saved and published anyway.

    Cheers,
    David

  • gerardo 3 posts 23 karma points
    Mar 05, 2014 @ 22:08
    gerardo
    0

    cool, just what i needed! did not know Document.BeforePublish is gone. thanks, 

    G- 

     

  • Avraham Kahana 22 posts 56 karma points
    Mar 03, 2015 @ 10:35
    Avraham Kahana
    0

    I came across this post, which definitely helped me - I ended up following the suggestion to use PublishingStrategy.Publishing event - which suits my needs. I just figured out an alternative way of doing it - I am now subscribing to ContentService.Saved (in ContentService there are lots of useful events, FYI), which also works for me.

    Is one way (PublishingStrategy vs ContentService) preferrable over the other ? Which is the newer, recommended approach, which follows umbraco's new paradigms ? I am using Umbraco 7.

    P.S. Here are the reasons for opting for ContentService instead of PublishingStrategy: 1) Looks more straightforward/clear to me 2 - the determinant factor :) Gives me the sender object, with which I have access to sender.Move method, which is what I ultimately need in my task.

    Thanks

    Update: Seems that the answer to my question is simply "they are 2 different events, but from the same umbraco (up-to-date) events model.

    public TurnIntoBlogEntry()
    {
        ContentService.Created += ContentService_Created;
        ContentService.Published += ContentService_Published;
    
    }
    

    Indeed, IPublishingStrategy does not provide me with a sender object that has "Move", the created event - which provides a sender object of type IContentService, does. That's all. So for my purposes, I should indeed pick a Created-like event (one that provides me with an interface that implements Move).

Please Sign in or register to post replies

Write your reply to:

Draft