Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andy 13 posts 54 karma points
    Apr 01, 2020 @ 08:34
    Andy
    0

    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!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Apr 02, 2020 @ 14:43
    David Brendel
    0

    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

  • Jonathon Cove 26 posts 101 karma points
    May 25, 2023 @ 16:08
    Jonathon Cove
    0

    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 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.

    Tested in Umbraco 10.5.1

Please Sign in or register to post replies

Write your reply to:

Draft