Copied to clipboard

Flag this post as spam?

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


  • Rajeev 126 posts 154 karma points
    Aug 31, 2011 @ 07:50
    Rajeev
    0

    Changing Document Property Value in Before Save

    Hello,

    I would like to see an example for changing document property value in the Before save event.

    Could any one help me on this.

     

    This is what I am currently doing and it is not getting the desired result.

    using System;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using Examine;

    public class UmbracoEvents: ApplicationBase
    {
    /// <summary>Constructor</summary>
    public UmbracoEvents()
    {
    Document.BeforeSave += new Document.SaveEventHandler(Document_BeforeSave);
    }

    private void Document_BeforeSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
    {
    if (sender.ContentType.Alias == "Article")
    {
    if(sender.getProperty("articleDate").Value.ToString().Trim().Length>0)
    {
    if(sender.getProperty("expiryDate").Value.ToString().Trim().Length==0)
    {

    DateTime startDate= (DateTime)sender.getProperty("articleDate").Value ;
    sender.getProperty("expiryDate").Value = startDate.AddMonths(12).AddDays(-1);
    sender.Save();
    }
    }
    }
    }
    }

    Any help on this will be appreciated.

    thanks,

    Rajeev

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 31, 2011 @ 11:12
    Bo Damgaard Mortensen
    0

    Hi Rajeev,

    Have you had a look at Richard Soetemans blog about it? :)

    http://www.richardsoeteman.net/PermaLink,guid,f470b6cf-40da-4aa9-a0d9-7b984fe9bf59.aspx

    Just out of curiousity: what is the result (if there is any) do you get from your code?

    - Bo

  • Rajeev 126 posts 154 karma points
    Aug 31, 2011 @ 11:37
    Rajeev
    0

    Hi Bo,

    Thank you for the udpate. I will look in to the blog.

    Nothing happening for now in save. I am not sure about if any mistakes in the code or any restriction for doing that.

    Thanks,

    Rajeev

     

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 31, 2011 @ 14:24
    Bo Damgaard Mortensen
    0

    Hi Rajeev,

    Have you tried to simply add something to the umbraco log table in the event? Just to see if the event actually fires :)

    umbraco.BusinessLogic.Log.Add(logtype, nodeid, comment);

    - Bo

  • Rajeev 126 posts 154 karma points
    Sep 02, 2011 @ 07:42
    Rajeev
    0

    Hi Bo,

    Apologies for the delay in updating as I was on something else, I have tried logging into the table.

    Actually in the Before Save event getProperty is not giving the values as desired.

    I am getting the values in the after save event and I am changing the values there now. Is there a way to refresh the UI in the event. The changes are visible only after reloading the nodes. Not sure if this is the right approach. Thank you again.

    This is the working code:

    Document.AfterSave += new Document.SaveEventHandler(Document_AfterSave);

     

     private void Document_AfterSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
      {
        if (sender.ContentType.Alias == "Article")
        {
          Log.Add(LogTypes.Custom, sender.Id, "After Save Event raised");
          if(sender.getProperty("articleDate").Value.ToString().Trim().Length>0 && sender.getProperty("expiryDate").Value.ToString().Trim().Length==0)
           {
            DateTime articleDate=Convert.ToDateTime(sender.getProperty("articleDate").Value.ToString());
            sender.getProperty("expiryDate").Value = articleDate.AddMonths(12).AddDays(-1);
            sender.Save();
           }
          }
      }

    Thank you Again.

    -Rajeev

Please Sign in or register to post replies

Write your reply to:

Draft