Content Service
Example on how to create and publish content programmatically using the `IContentService`.
Creating content programmatically
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Cms.Web.UI.Custom;
public class PublishContentDemo
{
private readonly IContentService _contentService;
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
public void Create()
{
// Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item.
var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
// Create a new child item of type 'Product'
var demoProduct = _contentService.Create("Microphone", parentId, "product");
// Set the value of the property with alias 'category'
demoProduct.SetValue("category" , "audio");
// Set the value of the property with alias 'price'
demoProduct.SetValue("price", "1500");
// Save content first
_contentService.Save(demoProduct);
// Publish content
var userId = 0; // 0 = system user
_contentService.Publish(demoProduct, new[] { "*" }, userId); // use "*" for invariant content
}
}Publishing content programmatically
Last updated
Was this helpful?