Copied to clipboard

Flag this post as spam?

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


  • Josh Reid 182 posts 258 karma points
    Jan 13, 2013 @ 21:03
    Josh Reid
    0

    Update record in action handler

    Hi guys

    I have been busy creating a 'member content management' type setup with Contour, and one of the key things is to keep the created node in sync with the record (using workflows in record > node & events/action handlers in node > record). I have written this as I cannot seem to update the record in an action handler (that is called on unpublish action- required so I can implement expire at date on the node).

    Full Action hanlder: (runs as expected, but the commented lines in meat of it cause it to fail):

    using System;
    using System.Collections.Generic;
    using System.Text;
    using umbraco.cms.businesslogic.web;
    using umbraco.BusinessLogic;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Services;
    using Umbraco.Forms.Data;
    using Umbraco.Forms.Data.Storage;
    namespace AdvertPublishing
    {
        /// <summary>
        /// Helps us with housekeeping - deleting node after (auto) unpublishing;
        /// </summary>
        public class UnPublishActionHandler : umbraco.BusinessLogic.Actions.IActionHandler
        {
            #region IActionHandler RemoveAdHandler

            public bool Execute(Document sender, umbraco.interfaces.IAction action) {
                /// <summary>
                /// have to use the action handler rather than unpublish event because the event doesn't get called when a document
                /// is being automatically expired (unpublished)
                /// </summary>
                if (action.Alias == umbraco.BusinessLogic.Actions.ActionUnPublish.Instance.Alias) {
                   
                    //Only applies for Advert
                    if (sender.ContentType.Alias == "Advert")
                    {
                        Log.Add(LogTypes.Custom, 0, "UnPublishActionHandler Executed in RemoveAdHandler");

                        //Check if content has recordGuid
                        if (!String.IsNullOrEmpty(sender.getProperty("recordGuid").Value.ToString()))
                        {
                            var storage = new RecordStorage();
                            Record record = storage.GetRecord(new Guid(sender.getProperty("recordGuid").Value.ToString()));
                            RecordService rs = new RecordService(record);
                            Form form = new FormStorage().GetFormFromRecordId(record.Id);

                            // Clear node from record
                            record.GetRecordField("Node").Values.Clear();
                            record.GetRecordField("Duration").Values.Clear();
                            storage.UpdateRecord(record, form);
                            //storage.UpdateRecordXml(record, form);
                            //storage.Dispose();
                            //rs.Submit();
                            //rs.Dispose();

                            Log.Add(LogTypes.Custom, 0, "record updated");

                            // Delete node
                            //sender.delete();
                            umbraco.library.RefreshContent();

                            Log.Add(LogTypes.Custom, 0, "node updated");                     }
                    }

                }
                return true;
            }

            public string HandlerName()
            {
                return "AdvertPublishing.UnPublishActionHandler";
            }

            public umbraco.interfaces.IAction[] ReturnActions() {
                return new umbraco.interfaces.IAction[] { umbraco.BusinessLogic.Actions.ActionUnPublish.Instance };
            }

            #endregion
        }

    }

     

    Relevant lines and why would these not work:

                            //storage.UpdateRecordXml(record, form);
                            //storage.Dispose();
                            //rs.Submit();
                            //rs.Dispose();

    Any thoughts, have I missed something (seemingly) obvious, or is there something with the context of an action handler that the contour record stoage etc won't work.

    Also while here, can you not delete a node from an upublish action? As that doesn't work either :(

    Thanks!
    Josh

  • Comment author was deleted

    Jan 14, 2013 @ 09:57

    Hi,

    Do you get any errors when those lines aren't commented out? If so could you provide the error messages

  • Josh Reid 182 posts 258 karma points
    Jan 14, 2013 @ 20:54
    Josh Reid
    0

    No errors, the script just doesnt make it to the following Log.Add - I wrapped the lot in a try catch, and logged the catch, but this didnt reveal anything either, the script still partially runs and just stops when hits something it does't like (sender.delete() also).

    Because it's running in an automated thread, there is no way to physically watch this happen (that I know of), can you suggest anything to debug it?

    But, at least you cant see anything obv wrong with code, or experienced similar before?

  • Comment author was deleted

    Jan 15, 2013 @ 09:58

    Don't see any issue with the code, you should be able to debug when attaching to the process and adding a breakpoint

  • Josh Reid 182 posts 258 karma points
    Jan 19, 2013 @ 10:47
    Josh Reid
    0

    Hi Tim

    I got back to this again, and looks like the record stuff needs HttpContext / umbracoContext (which can't be available from the event handlers??) - with error thrown by the

    storage.UpdateRecordXml(record,form);

    is as follows:

    System.ArgumentNullException: Value cannot be null.
    Parameter name: umbracoContext
       at Umbraco.Web.UmbracoHelper..ctor(UmbracoContext umbracoContext)
       at Umbraco.Forms.Core.Record.ToXml(XmlDocument xd)
       at Umbraco.Forms.Data.Storage.RecordStorage.UpdateRecordXml(Record record, Form form)
       at AdvertPublishing.UnPublishActionHandler.Execute(Document sender, IAction action)

    Further the RecordService also fails if in the unpublish event handler:

    System.NullReferenceException:
     The HttpContext property has not been set or the object execution is 
    not running inside of an HttpContext
       at umbraco.BusinessLogic.StateHelper.get_HttpContext()
       at umbraco.BusinessLogic.StateHelper.Cookies.get_HasCookies()
       at umbraco.BusinessLogic.StateHelper.SetCookieValue(String key, 
    String value, Double daysToPersist)
       at Umbraco.Forms.Core.Services.RecordService.()
       at Umbraco.Forms.Core.Services.RecordService.Submit()
       at 
    AdvertPublishing.UnpublishAdHandler.Document_AfterUnPublish(Document 
    sender, UnPublishEventArgs e)
  • Comment author was deleted

    Jan 20, 2013 @ 15:04

    Ok thanks for the details will add this to our issue tracker and take a look at it for the next maintenance release...

  • Comment author was deleted

    Jan 20, 2013 @ 15:08
  • Comment author was deleted

    Jan 20, 2013 @ 15:09

    As a temp workaround you could place the code on a seperate page and then request that page from the action handler, not ideal but a fast workaround...

  • Josh Reid 182 posts 258 karma points
    Jan 21, 2013 @ 00:00
    Josh Reid
    0

    Thanks Tim, that's where I was headed (as you say not ideal, but will have to do meantime).

    Please update when there is resolution and I'll mark the thread as solved ;)

Please Sign in or register to post replies

Write your reply to:

Draft