Copied to clipboard

Flag this post as spam?

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


  • Pavel Gabor 13 posts 53 karma points
    Nov 24, 2014 @ 21:07
    Pavel Gabor
    0

    How to get content property value in Saving / Saved / Publishing / Published events?

    I would like to get property values in Umbraco ApplicationEventHandlers.

    I'm using Umbraco v.: 7.18


    My code looks like:

     

    public class UmbracoEventHandlers: ApplicationEventHandler

    {

    public UmbracoEventHandlers()

    {

    ContentService.Saving += ContentService_Saving;

    ContentService.Saved += ContentService_Saved;

    ContentService.Publishing += ContentService_Publishing;

    }

     

     void ContentService_Saved(IContentService sender, SaveEventArgs e)

    {

     foreach (var node in e.SavedEntities)

     {

    if (node.ContentType.Alias == "documentType")

    {

     string emailTemplateName = node.GetValue("templateName");

     

     


    GetValue method call always throws error:

     

    An error occurred

    Object reference not set to an instance of an object.

     

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

     

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

     

    Am I missing something or did I take wrong approach to achieve desired goal?

     

    Any help would be appreciated.

  • Alex Skrypnyk 6147 posts 24056 karma points MVP 8x admin c-trib
    Nov 24, 2014 @ 23:59
    Alex Skrypnyk
    0

    Hi Pavel,

    Did you read that article ?

    http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events

    About Saving event :

    Raised when ContentService.Save is called in the API. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IContentService object. "e" will provide: NOTE: If the entity is brand new then HasIdentity will equal false. SavedEntities: Gets the collection of IContent objects being saved.

    How did you call Save method ?

    Thanks

  • jivan thapa 194 posts 681 karma points
    Nov 25, 2014 @ 07:29
    jivan thapa
    100

    NullReferenceException might be caused by a Content that does not have a property alias "templateName". It's better to check the property before retrieve the value.

       if (node.ContentType.Alias == "documentType")
                {
                    if (node.HasProperty("templateName"))
                    {
                        var emailTemplateName = node.GetValue("templateName").ToString();
                    }
                }
    
  • Pavel Gabor 13 posts 53 karma points
    Nov 25, 2014 @ 09:29
    Pavel Gabor
    0

    Thank you both for help.

     

    It turns out, that I was trying to retrieve property value with wrong property alias. I do not know why, but all property aliases on document type were written in lower case, not in camel case as they usually are.

Please Sign in or register to post replies

Write your reply to:

Draft