Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Created June 18, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepollock/f5d4c89766433e402383 to your computer and use it in GitHub Desktop.
Save jamiepollock/f5d4c89766433e402383 to your computer and use it in GitHub Desktop.
Tool for automatically resaving media nodes in Umbraco v6. Add to your dashboard.config to gain easy access to this tool.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RebuildMediaCacheTool.ascx.cs" Inherits="My.Website.Dashboard.RebuildMediaCacheTool" %>
<div class="dashboardWrapper">
<h2>Rebuild Media Cache</h2>
<img src="/umbraco_client/Dashboards/ExamineManagementIco.png" alt="" class="dashboardIcon" />
<asp:MultiView ID="MainView" runat="server">
<asp:View ID="InitialView" runat="server">
<asp:Button ID="RebuildButton" Text="Rebuild Media Cache" runat="server"/>
</asp:View>
<asp:View ID="CompleteView" runat="server">
<p>Rebuild complete!</p>
<p><%: MediaCount %> Media Items cache rebuilt</p>
</asp:View>
</asp:MultiView>
</div>
using System;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Web.UI.Controls;
namespace My.Website.Dashboard {
public partial class RebuildMediaCacheTool : UmbracoUserControl {
public int MediaCount { get; set; }
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack == false) {
MainView.SetActiveView(InitialView);
}
RebuildButton.Click += OnRebuildButtonClick;
}
private void OnRebuildButtonClick(object sender, EventArgs e) {
var rootNodes = Services.MediaService.GetRootMedia();
foreach (var rootNode in rootNodes.Where(x => x.Trashed == false)) {
Services.MediaService.Save(rootNode);
MediaCount++;
foreach (var ancestor in rootNode.Descendants().Where(x => x.Trashed == false)) {
Services.MediaService.Save(ancestor);
MediaCount++;
}
}
MainView.SetActiveView(CompleteView);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment