Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Sep 05, 2016 @ 07:26
    Chriztian Steinmeier
    0

    How to clean data in a specific property on multiple documents?

    Hi all,

    I've recently had to change a property from a simplified RTE (enabled bold, italic & links) to plain text by request, to get rid of pasted <table>, <canvas>, <iframe> etc.

    The frontend never showed any of this (I have a way of totally killing off those pesterings), and still doesn't - but of course, now the fields show the raw HTML tags in the Umbraco Backoffice because of the change of type.

    I would like to do some cleaning with a one-time overhaul (basically calling StripHtml() on the fields to get rid of the tags. (Most fields are just wrapped in a <p>aragraph because most editors behave :-)

    What's the best approach to this?

    /Chriztian

  • Kevin Jump 2348 posts 14896 karma points MVP 8x c-trib
    Sep 05, 2016 @ 08:37
    Kevin Jump
    100

    Hi Chriztian,

    for a quick fix i would create a class, that goes to the back office services, loops through all the content, looking for the property - doing the fix and saving (or publishing) the changes.

    if you make this class an UmbracoAuthorizedApiController, then you can just call it from a URL (once you are logged onto the back office)

    e.g.

     using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Core.Logging;
    using Umbraco.Web.Mvc;
    
    public class PropertyFix : UmbracoAuthorizedController 
    {
    
        public void FixContent()
        {
            var _contentService = ApplicationContext.Current.Services.ContentService;
    
            var rootContent = _contentService.GetRootContent();
    
            foreach(var item in rootContent)
            {
                FixContentItem(_contentService, item);
            }
        }
    
        public void FixContentItem(IContentService contentService, IContent item)
        {
            if (item.HasProperty("property") )
            {
                var p = item.GetValue<string>("property");
    
                // clean up property here.
    
    
                LogHelper.Info<PropertyFix>("Fixing Property on {0}", () => item.Name);
                item.SetValue("property", p);
    
                if (item.Published)
                    contentService.Save(item);
                else
                    contentService.SaveAndPublishWithStatus(item);
            }
    
            // do the children
            foreach(var child in item.Children())
            {
                FixContentItem(contentService, child);
            }
        }
    }
    

    you could then call FixContent from

    /Umbraco/backoffice/Api/PropertyFix/FixContent

    and you would see some logging in the umbraco log (app_data/logs)

    just a note i haven't ran the code above! - test (and change ) it on something before you do your live site!!

    K

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Sep 05, 2016 @ 08:47
    Chriztian Steinmeier
    0

    Thanks Kevin!

    Looks like a good way to go about it; will try it on the live site and hold you responsible for any hiccups #justkidding :-)

    Sidenote: I actually changed from the simplified RTE to the much nicer "Styled Textbox" :-)

    /Chriztian

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies