Created
April 10, 2025 12:18
-
-
Save Rockerby/655d0c32d134c81f7b63c52b61e1045a to your computer and use it in GitHub Desktop.
An Umbraco (13.8+) Controller that re-publishes content and it's descendants. Useful for if you've made database changes and need the site to puiblish them without going into each.
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 Microsoft.AspNetCore.Mvc; | |
| using Umbraco.Cms.Core.Models; | |
| using Umbraco.Cms.Core.Services; | |
| using Umbraco.Cms.Web.Common.Controllers; | |
| namespace Website.Code | |
| { | |
| public class PublishDescendantsController : UmbracoApiController | |
| { | |
| private readonly IContentService _contentService; | |
| private readonly ILogger _logger; | |
| public PublishDescendantsController(IContentService contentService, | |
| ILogger<PublishDescendantsController> logger) | |
| { | |
| _contentService = contentService; | |
| _logger = logger; | |
| } | |
| // POST me at /umbraco/PublishDescendants/PublishWithDescendants with a payload of { "key": "A GUID!" } | |
| public async Task<IActionResult> PublishWithDescendants([FromBody] PublishReq req) | |
| { | |
| _logger.LogInformation("Publish with descendants call made for Key {key}", req.Key.ToString()); | |
| IContent? content = _contentService.GetById(req.Key) | |
| ?? throw new InvalidOperationException($"Could not find content with key: {req.Key}."); | |
| var results = _contentService.SaveAndPublishBranch(content, PublishBranchFilter.ForceRepublish); | |
| return new JsonResult(results); | |
| } | |
| } | |
| public class PublishReq | |
| { | |
| public Guid Key { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment