Copied to clipboard

Flag this post as spam?

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


  • Paul Yates 32 posts 86 karma points
    Aug 13, 2014 @ 17:40
    Paul Yates
    0

    Adding new properties to media type when there are existing media items

    If i add a new property to a media type (in this case, Image) it doesn't become available to use in code until i've resaved the media item (clicked on save on the image in the CMS).  

    When there are more than a few images this obviously becomes very tedious.  Is there a way to re-save all images so i don't have to do this?    This feels like a bug...?

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 15, 2014 @ 09:36
    Dan Lister
    100

    Hi Paul,

    Its a bit hacky but to resave all media items, I've previously created a temporary template which has the following code. It finds all root media items and recursively finds each child, saving each one along the way.

    @using umbraco.cms.businesslogic.media
    @{
        SaveMedias(Media.GetRootMedias());
    }
    
    @functions
    {
        public void SaveMedias(Media[] medias)
        {
            if (medias == null || !medias.Any()) 
                return;
    
            foreach (var media in medias)
            {
                Response.Write("Saving " + media.Text + "/n");
                media.Save();
                SaveMedias(media.Children);
            }
        }
    }
    

    Thanks, Dan.

  • Paul Yates 32 posts 86 karma points
    Aug 16, 2014 @ 02:09
    Paul Yates
    0

    Thats brilliant thanks!  So obvious as well, kicking myself i didn't i think of it!! 

Please Sign in or register to post replies

Write your reply to:

Draft