Copied to clipboard

Flag this post as spam?

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


  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 15, 2013 @ 21:14
    Jeavon Leopold
    0

    ContentService help please

    Hi All,

    I am currently hooking into the content service to grab a property run it though embed.ly and then save the code back into a property, it's working pretty well, however, I've two issues I need a little help with:

    1. I am binding only the save event, however when a Umbraco users Saves and Publishes, the in memory cache is not being updated with the new values (right clicking then publish entire site sorts it out). I am wondering if this is because my call to the .Save has raiseEvents false, but I can't have a save event triggering a save event as it will never end....?
    2. In order to get the UI to refresh with the new value, I am having to use the ClientTools.ChangeContentFrameUrl method however this causes the old double reload and also if the user was on a specific tab, they are taken back to the beginning, is there a better way?

    My code:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saved += ContentService_Saved;
        }
        private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
        {
            int? lastItemId = null;                       
            foreach (var contentDoc in e.SavedEntities)
            {
                if (contentDoc != null && contentDoc.ContentType.Alias == "Pin")
                {
                    if (contentDoc.Equals(e.SavedEntities.Last()))
                    {
                        lastItemId = contentDoc.Id;
                    }
                    if (contentDoc.HasProperty("mediaUrl") && !String.IsNullOrEmpty(contentDoc.GetValue<string>("mediaUrl")))
                    {
                        var embedlyCode = Embedly.Helper.GetEmbed(contentDoc.GetValue<string>("mediaUrl"));
                        if (embedlyCode != null)
                        {
                            contentDoc.SetValue("embedCode", embedlyCode);
                        }
                        else
                        {
                            contentDoc.SetValue("embedCode", String.Empty);
                        }                        
                        sender.Save(contentDoc, 0, false);
                        var clientTools = new Umbraco.Web.UI.Pages.ClientTools((Page)HttpContext.Current.CurrentHandler);
                        clientTools.ChangeContentFrameUrl(string.Concat("editContent.aspx?id=", lastItemId));
                    }
                }
            }
        }
    }
    

    Any suggestions greatly appreciated!

    Thanks!

    Jeavon

  • andrew shearer 510 posts 659 karma points
    Aug 16, 2013 @ 04:02
    andrew shearer
    0

    hi Jeavon, it sounds like you want to use the SaveAndPublish() method on the content service rather than just Save()?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 16, 2013 @ 10:09
    Jeavon Leopold
    0

    Hi Andrew, not really as that would then publish the content even if the user had only saved. I have just realised that the solution to point 1 was to use the .Saving event instead of .Saved as that is equivalent to the v4 BeforeSave event.

    Still stumped on point 2 though...?

    Thanks!

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 16, 2013 @ 11:33
    Stefan Kip
    0

    I can't think of a better way for point 2. It's because the browser remembers the form values entered before.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 16, 2013 @ 11:47
    Jeavon Leopold
    0

    Yeah, it would be nice to at least go back to the right tab. I think I can probably find which tab the property is on from the ContentTypeComposition but I can't see any method on clientTools to setActiveTab or similar....?

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Aug 16, 2013 @ 12:27
    Morten Christensen
    100

    Hi Jeavon,

    #1 Why not use the Saving event ? If its just a property value you want to update then you can just "inject" that before the Content is Saved or Saved and Published. Would save you from double saving as far as I can tell.
    The in memory cache should be updated regardless of which and how many saves you are doing. As soon as you hit "update" through the repositories (the level below the ContentService) the in memory cache is updated.

    #2 I don't think there is a much better way to do it. The issue is unfortunately because of the legacy code used in the Backoffice, which loads a Document OnInit. This Document is used by the ContentControl, which renders out all the fields and the values within them. The Document has an internal version of the new IContent object, which is updated based on the changes made through the editor and then passed on to the ContentService for saving. When you are updating the Content through events these updates are not really fed back into the ContentControl because its using a Document (with the IContent object behind the scenes) that is already loaded.
    So yea the only way to have the content editor (frame) updated with changes made through events would be to reload the content frame. Only other alternative would be to override the editContent.aspx and ensure that the loaded ContentControl is fed the newly updated IContent object. That would be a bit more hacky then what you are currently doing.

    Altough....there is another thing that might be worth a shot. The legacy Document.Before save event might actually be a better fit. If you update the property through the document.GenericProperties collection then I believe you would be updating the instance of the object that is loaded in editContent and the ContentControl, so that the updated value would be shown. But im not 100% certain, but worth a try.

    Hope this helps,

    Morten Christensen

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 16, 2013 @ 12:36
    Jeavon Leopold
    0

    Thanks Morten!

    1. Yeah, realised that one this morning, and it's working perfectly :-)
    2. Hmm, interesting something you can do in the v4 Api you can't do in the v6 Api! It would be really useful if there was a clientTools.SetActiveTab method or similar though, c'est la vie :-) I'll wait for v7....

    Jeavon

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Aug 16, 2013 @ 12:52
    Morten Christensen
    0

    On second thought I'm not really sure that is the case. As Mr.Kipusoep mentioned its most likely because the browser remembers the values - until you do a "hard" refresh.

    - Morten

  • Tom 713 posts 954 karma points
    Sep 23, 2013 @ 07:42
    Tom
    0

    Just wondering what you ended up doing? I am using ContentService.Saving but the value isn't refreshed as you experienced..

    Is there a way to force a refresh?

    Thanks,

    Tom

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 23, 2013 @ 11:38
    Jeavon Leopold
    0

    Hi Tom,

    I ended up using the reload "hack", obviously the users ends up on the first tab

    sender.Save(contentDoc, 0, false);
    if (HttpContext.Current != null)
    {
        var clientTools = new Umbraco.Web.UI.Pages.ClientTools((Page)HttpContext.Current.CurrentHandler);
        clientTools.ChangeContentFrameUrl(string.Concat("editContent.aspx?id=", lastItemId));
    }
    

    Hope that helps you.

    Jeavon

  • Tom 713 posts 954 karma points
    Sep 24, 2013 @ 00:27
    Tom
    0

    Thanks Jeavon,

    that worked perfectly.

    Cheers,

    Tom

  • Scott McClannahan 10 posts 30 karma points
    Feb 13, 2014 @ 02:28
    Scott McClannahan
    0

    Can someone please post and let me know if this (Tom's hack above) is still the best way to handle this in v6.1 ??

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Feb 13, 2014 @ 08:19
    Jeavon Leopold
    0

    Hi Scott,

    Yes, the reload "hack" I posted is the only method for v6.1.x. I haven't looked to see how this works in v7 yet but hopefully there might be something nicer....

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft