Created
March 2, 2020 18:33
-
-
Save calvindavis/3b6d2572041a62e69179c827557578e0 to your computer and use it in GitHub Desktop.
Save all media in Umbraco 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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