Copied to clipboard

Flag this post as spam?

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


  • Robin Crama 5 posts 56 karma points
    Sep 10, 2014 @ 10:53
    Robin Crama
    0

    Save all media items button

    Anyone had the same urge to save all media items at the same time?
    We had to do this a couple of times now after deploy, or change in doctype of the media items.
    (for instance after introducing the image cropper in media items)

    Very annoying to have to click hundereds of save buttons....

    Anyone ideas to do this at once?

    Thanks!
    Robin.

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 10, 2014 @ 11:27
    Dan Lister
    0

    Hi Robin,

    I've got round the problem previously by creating a temporary template which loops through all media items, saving each one as it goes along.

    @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.

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Sep 10, 2014 @ 11:30
    Richard Soeteman
    0

    Hi Robin,

    Maybe my Bulkmanager can help http://soetemansoftware.nl/bulkmanager. I can easily add an extra option to Bulk save selected items. I already have an additional option to regenerate crops.

    Maybe something you can use?

    Best,

    Richard

  • Robin Crama 5 posts 56 karma points
    Sep 10, 2014 @ 11:45
    Robin Crama
    0

    @Dan, testing it right now Txs!

    @Richard, heard of it, will test the bulkmanager tonight! Txs! 

  • Robin Crama 5 posts 56 karma points
    Sep 10, 2014 @ 12:47
    Robin Crama
    101

    @Dan, txs for the code. ended up with the following and works like a charm:

    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;
    
    @{
        var mediaService = ApplicationContext.Current.Services.MediaService;
    
        SaveMedias(mediaService.GetRootMedia().ToList());
    }
    
    @functions
    {
        public void SaveMedias(List<IMedia> medias)
        {
            var _mediaService = ApplicationContext.Current.Services.MediaService;
    
            if (medias == null || !medias.Any()) 
                return;
    
            foreach (IMedia media in medias)
            {
                _mediaService.Save(media);
    
                SaveMedias(media.Children().ToList());
    
                Response.Write(media.Name + " saved at:" + media.UpdateDate + "<br />");
    
            }
        }
    }
    
  • Iain Miller 1 post 21 karma points
    Jan 13, 2016 @ 13:34
    Iain Miller
    0

    @Robin and @Dan,

    We had renamed a MediaType from MediaFile to File. And to update exising data replaced the markup in one of our existing views with your code. Navigated to that view. All my mediaTypes were updated and this was confirmed on screen. We reverted back to our original template markup and tested our changes.

    We were able to use this process on our hosted sites.

    All went well. Many thanks.

  • Jamie Attwood 208 posts 504 karma points c-trib
    Jan 08, 2015 @ 18:04
    Jamie Attwood
    0

    Sadly I am still stuck in the land of webforms for a project that I am working on but need this functionality.

    Do you have an equivilent snippit that would work in the page load event of a

    <script runat="server"> 

    embedded in a master page template? Any help appreciated!

    <script type="text/javascript">// <![CDATA[
     embedded in a master page template? Any help appreciated!
    // ]]></script>
  • Jamie Attwood 208 posts 504 karma points c-trib
    Jan 14, 2015 @ 15:51
    Jamie Attwood
    1

    Thanks, Robin, I repurposed your code to use in a web form. For those still using forms, you can create a new template and assign it to a generic page. When you hit that page the script will execute. 

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
    <%@ Import Namespace="Umbraco.Core" %>
    <%@ Import Namespace="Umbraco.Core.Services" %>   
    <%@ Import Namespace="Umbraco.Core.Models" %>
    
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        var mediaService = ApplicationContext.Current.Services.MediaService;
    
        //SaveMedias(mediaService.GetRootMedia().ToList()); //Save ALL media
        SaveMedias(mediaService.GetChildren(9146).ToList()); //Save all children of a specific parent node
    }
    
    public void SaveMedias(List<IMedia> medias)
        {
            var _mediaService = ApplicationContext.Current.Services.MediaService;
    
            if (medias == null || !medias.Any()) 
                return;
    
            foreach (IMedia media in medias)
            {
                _mediaService.Save(media);
    
                SaveMedias(media.Children().ToList());
    
                Response.Write(media.Name + " saved at:" + media.UpdateDate + "<br />");
    
            }
        }       
    </script> 
    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server"></asp:Content>
Please Sign in or register to post replies

Write your reply to:

Draft