Skip to content

Instantly share code, notes, and snippets.

@manutdkid77
Created November 23, 2025 06:15
Show Gist options
  • Select an option

  • Save manutdkid77/fef2db21ea678301ff6984cc5fe54f50 to your computer and use it in GitHub Desktop.

Select an option

Save manutdkid77/fef2db21ea678301ff6984cc5fe54f50 to your computer and use it in GitHub Desktop.
Umbraco 13 BlockList Import
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Controllers;
namespace UmbraTest13.Controllers
{
public class TimelineImportController : UmbracoApiController
{
readonly IContentService _contentService;
readonly IContentTypeService _contentTypeService;
public TimelineImportController(IContentService contentService, IContentTypeService contentTypeService)
{
_contentService = contentService;
_contentTypeService = contentTypeService;
}
[HttpPost]
public ActionResult Import([FromBody] ImportModel importModel)
{
// Validate PageGuid
if (!Guid.TryParse(importModel.PageGuid, out var pageId))
return BadRequest("Invalid pageGuid value.");
// Fetch page by ID
var page = _contentService.GetById(pageId);
// Ensure page exists
if (page == null)
return NotFound("Page not found.");
// Check if property alias exists
if (!page.Properties.Contains(importModel.BlockListAlias))
return BadRequest($"Property '{importModel.BlockListAlias}' does not exist.");
// Get existing blocklist JSON
var rawJsonBlock = page.GetValue<string>("timelineItems");
BlockValue blockValue = new BlockValue();
// Deserialize existing blocklist if present
if (!string.IsNullOrWhiteSpace(rawJsonBlock))
blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJsonBlock) ?? new BlockValue();
// Get all content types
var contentTypes = _contentTypeService.GetAll();
// Find timelineItem content type
var timelineItemType = contentTypes.Where(n => n.Alias == "timelineItem").FirstOrDefault();
if (timelineItemType == null)
return NotFound("timelineItem Content Type not found.");
// Map incoming timelines to BlockItemData
var timelines = importModel.Timelines.Select(x => new BlockItemData()
{
ContentTypeAlias = "", // Alias not needed here
ContentTypeKey = timelineItemType.Key,
RawPropertyValues = new Dictionary<string, object?>()
{
{ "eventName", x.Name },
{ "period", $"{x.StartDate} - {x.EndDate}" },
},
Udi = Udi.Create("element", Guid.NewGuid())
}).ToList();
// Add new items to blockValue
blockValue.ContentData.AddRange(timelines);
// Build layout JSON for blocklist
var Layout = new Dictionary<string, JToken>
{
["Umbraco.BlockList"] = new JArray(
blockValue.ContentData.Select(item =>
new JObject
{
["contentUdi"] = item?.Udi?.ToString()
}
)
)
};
// Assign layout back to blockValue
blockValue.Layout = Layout;
// Save updated blocklist to page property
page.Properties["timelineItems"]?.SetValue(JsonConvert.SerializeObject(blockValue));
// Save and publish if page has changes
if (page.IsDirty())
{
_contentService.SaveAndPublish(page);
}
// Return success response
return Ok("Timeline items injected successfully.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment