Skip to content

Instantly share code, notes, and snippets.

@calvindavis
Created March 2, 2020 18:33
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 calvindavis/3b6d2572041a62e69179c827557578e0 to your computer and use it in GitHub Desktop.
Save calvindavis/3b6d2572041a62e69179c827557578e0 to your computer and use it in GitHub Desktop.
Save all media in Umbraco 8
using System.Web.Http;
using Umbraco.Core.Services;
using Umbraco.Web.WebApi;
namespace Yoyo.Elevator.Core.Controllers
{
public class SaveMediaController : UmbracoApiController
{
private readonly IMediaService _mediaService;
public SaveMediaController(IMediaService mediaService)
{
_mediaService = mediaService;
}
[HttpGet]
// /umbraco/api/savemedia/save
public void Save()
{
var rootMedia = _mediaService.GetRootMedia();
foreach (var media in rootMedia)
{
_mediaService.Save(media);
var descendants = _mediaService.GetPagedDescendants(media.Id, 0, int.MaxValue, out long totalRecords);
foreach (var descendant in descendants)
{
_mediaService.Save(descendant);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment