So i'm brand new to umbraco and very excited to be using it. I've completed the online getting started tutorial that Umbraco provide and now i'm looking to delve a little deeper.
Trying to understand how to create a custom document type from code, rather than use the out of the box feature's id like to know how to create my own.
Can someone provide some info / a link to how I might go about this? I'm certain there is stuff out there but I can't seem to find it! I'm still new so i'm probably not looking in the right place!
I'm not sure about v8, but I can give you an example for v10
To create a Document Type in code, you'll need to use the ContentTypeService (Document Types were known as Content Types in previous version of Umbraco, so the service still has the old name).
You'll need nuget packages Umbraco.Cms.Core and Umbraco.Cms.Web.Backoffice at minimum
The below example creates a container (folder) called 'Test' and creates a new content type within it called "New Content Type".
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Web.BackOffice.Controllers;
namespace SpektrixHelper.Controllers
{
public class TestController : UmbracoAuthorizedApiController
{
public readonly IContentTypeService _contentTypeService;
public readonly IShortStringHelper _shortStringHelper;
public TestController(ISpektrixSync spektrixService, IContentTypeService contentTypeService, IShortStringHelper shorty)
{
_contentTypeService = contentTypeService;
_shortStringHelper = shorty;
}
public ActionResult DoSomething()
{
//default level is 1
var container = _contentTypeService.GetContainers("Test", 1)?.FirstOrDefault();
if (container == null)
{
//-1 = the parent id
var attempt = _contentTypeService.CreateContainer(-1, Guid.NewGuid(), "Test");
var result = attempt.Result.Entity;
_contentTypeService.SaveContainer(result);
}
IContentType newCT = _contentTypeService.Get("New Content Type");
if (newCT == null)
{
newCT = new ContentType(_shortStringHelper, container.Id) { Alias = "newContentType", Icon = "icon-clothes-hanger color-deep-purple" };
newCT.Name = "New Content Type";
_contentTypeService.Save(newCT);
}
return Ok();
}
}
}
Once you've installed the necessary packages, you should be able to go to the URL /umbraco/backoffice/api/SyncTest/DoSomething , to run the code.
How to create a custom document type from code?
Hi all,
So i'm brand new to umbraco and very excited to be using it. I've completed the online getting started tutorial that Umbraco provide and now i'm looking to delve a little deeper.
Trying to understand how to create a custom document type from code, rather than use the out of the box feature's id like to know how to create my own.
Can someone provide some info / a link to how I might go about this? I'm certain there is stuff out there but I can't seem to find it! I'm still new so i'm probably not looking in the right place!
Thanks!
Hi Andy,
in Umbraco you usually don't create DocumentTypes by Code. You are using the UI in the CMS.
There is a IContentTypeService which you can inject into your controller/class and use.
But when you are trying to create your site structure then as said use the UI.
Regards David
Hi
I'm not sure about v8, but I can give you an example for v10
To create a Document Type in code, you'll need to use the ContentTypeService (Document Types were known as Content Types in previous version of Umbraco, so the service still has the old name).
You'll need nuget packages
Umbraco.Cms.Core
andUmbraco.Cms.Web.Backoffice
at minimumThe below example creates a container (folder) called 'Test' and creates a new content type within it called "New Content Type".
Once you've installed the necessary packages, you should be able to go to the URL /umbraco/backoffice/api/SyncTest/DoSomething , to run the code.
Tested in Umbraco 10.5.1
is working on a reply...