# Create Document Type programmatically

**URL:** <https://forum.umbraco.com/t/create-document-type-programmatically/5702>\
**Category:** Questions\
**Tags:** v16\
**Created:** [September 4, 2025, 8:05pm UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702 "2025-09-04T20:05:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spleeding1](https://forum.umbraco.com/user_avatar/forum.umbraco.com/spleeding1/32/1285_2.png) [@Spleeding1](https://forum.umbraco.com/u/Spleeding1)\
**Post date:** [September 4, 2025, 8:05pm UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/1 "2025-09-04T20:05:41Z")

</div>

Is there a way to create a Document Type through code? I’m sure there is, I just can’t find the resource for how to do it.

---

<div class="post-metadata">

**Author:** ![Saeve](https://forum.umbraco.com/user_avatar/forum.umbraco.com/saeve/32/405_2.png) [@Saeve](https://forum.umbraco.com/u/Saeve)\
**Post date:** [September 5, 2025, 6:41am UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/2 "2025-09-05T06:41:27Z")

</div>

Hi Carl 🙂

Yes there is most certainly a way to do it.  
But I wonder for what reason this might be applicable?  
Modern Umbraco has many ways and tools to create content types and more like with the Management API.

In any case, raw code docs for Content Types can be found here:

> **[Class ContentTypeServiceBase
   | Umbraco c# Api docs](https://apidocs.umbraco.com/v16/csharp/api/Umbraco.Cms.Core.Services.ContentTypeServiceBase-2.html)**

There’ is also a great Skrift post about working with Content Types on code:

> **[Working with Content Types and Data Types Programmatically by Anders Bjerner...](https://skrift.io/issues/working-with-content-types-and-data-types-programmatically/)**
>
> The Umbraco backoffice is a great tool for managing content types and data types, but sometimes as developers we need to take a more hands-on approach. In this comprehensive guide, Anders walks us through the process of creating and managing content...

These should be able to get you started.

---

<div class="post-metadata">

**Author:** ![EssCee](https://forum.umbraco.com/user_avatar/forum.umbraco.com/esscee/32/3305_2.png) [@EssCee](https://forum.umbraco.com/u/EssCee)\
**Post date:** [September 5, 2025, 7:03am UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/3 "2025-09-05T07:03:14Z")

</div>

A quick example to get you going (of course I don’t know your structure but reach back out if you need more information)

Create a class

```auto
public class DocumentTypeCreation(
    IContentTypeService ContentTypeService,
    IShortStringHelper ShortStringHelper)
{
    public void CreateDocType()
    {
        var docType = ContentTypeService.Get("myDocTypeAlias");
        if (docType == null)
        {
            var newDocType = new Umbraco.Cms.Core.Models.ContentType(ShortStringHelper, -1)
            {
                Name = "My Doc Type",
                Alias = "myDocTypeAlias",
                Description = "A description for my doc type",
                Icon = "icon-document",
                Thumbnail = "doc.png",
                AllowedAsRoot = true
            };

            ContentTypeService.Save(newDocType);
        }
 
    }
}

```

Register your class

```auto
public class ServiceComposers : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.Services.AddScoped<DocumentTypeCreation>();
    }
}

```

In your razor view add the below code to execute

```auto
@inject DocumentTypeCreation docTypeCreate;
@{
	Layout = null;
	docTypeCreate.CreateDocType();
}

```

Run the page and you should see the document type in the backoffice

 ![image](https://forum.umbraco.com/uploads/default/original/2X/e/e1dd01a6cf428349edeb3172283f16a8fd00985a.png)

The above is a simple demo to get you started, so please refactor accordingly using correct events as to when the document type should be created.

---

<div class="post-metadata">

**Author:** ![Luuk](https://forum.umbraco.com/user_avatar/forum.umbraco.com/luuk/32/4446_2.png) [@Luuk](https://forum.umbraco.com/u/Luuk)\
**Post date:** [September 5, 2025, 7:25am UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/4 "2025-09-05T07:25:39Z")

</div>

This is a helper that we use for creating document types. It also features the ability to add tabs. It takes a custom tabs definition, so you can’t use it as-is, but I think you get the idea from looking at the code:

```csharp
	public async Task<IContentType?> CreateContentType(int parentId, string alias, string name, string description,
		string icon,
		IEnumerable<ContentTypeSort>? allowedContentTypes, IEnumerable<string>? compositionAliases,
		IEnumerable<ITabTypeDefinition>? tabs = null,
		bool isElementType = false, Guid? userId = null)
	{
		try
		{
			var executingUserId = userId ?? UmbracoCore.Constants.Security.SuperUserKey;

			var result = new ContentType(_shortStringHelper, parentId)
			{
				Alias = alias,
				Description = description,
				Icon = icon,
				Name = name,
				IsElement = isElementType
			};

			if (allowedContentTypes != null)
				result.AllowedContentTypes = allowedContentTypes;

			var compositions = GetCompositionsFromAlias(compositionAliases);
			if (compositions != null)
				result.ContentTypeComposition = compositions;

			if (tabs != null)
			{
				foreach (var tabTypeDefinition in tabs)
				{
					var tab = new PropertyGroup(new PropertyTypeCollection(result.SupportsPublishing))
					{
						Alias = tabTypeDefinition.Alias,
						Name = tabTypeDefinition.Name,
						Type = PropertyGroupType.Tab,
						SortOrder = tabTypeDefinition.SortOrder
					};

					if (tabTypeDefinition.Groups != null && tabTypeDefinition.Groups.Any())
					{
						foreach (var groupTypeDefinition in tabTypeDefinition.Groups)
						{
							var group = new PropertyGroup(new PropertyTypeCollection(result.SupportsPublishing))
							{
								Alias = $"{tab.Alias}/{groupTypeDefinition.Alias}",
								Name = groupTypeDefinition.Name,
								Type = PropertyGroupType.Group,
								SortOrder = groupTypeDefinition.SortOrder,
							};

							if (groupTypeDefinition.DataTypes != null && groupTypeDefinition.DataTypes.Any())
							{
								foreach (var dataTypeDefinition in groupTypeDefinition.DataTypes)
								{
									var newDataType = await GetPropertyType(dataTypeDefinition);
									if (newDataType != null)
										group.PropertyTypes?.Add(newDataType);
								}
							}

							result.PropertyGroups.Add(group);
						}
					}

					result.PropertyGroups.Add(tab);
				}
			}

			await _contentTypeService.UpdateAsync(result, executingUserId);

			return result;
		}
		catch (Exception ex)
		{
			_logger.LogError(ex, "Unhandled exception when creating content type with name {name} and alias {alias}",
					name, alias);
			return null;
		}
	}

```

---

<div class="post-metadata">

**Author:** ![Spleeding1](https://forum.umbraco.com/user_avatar/forum.umbraco.com/spleeding1/32/1285_2.png) [@Spleeding1](https://forum.umbraco.com/u/Spleeding1)\
**Post date:** [September 5, 2025, 12:15pm UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/5 "2025-09-05T12:15:09Z")

</div>

Thanks for the resources. I want it for an import feature that I need.

---

<div class="post-metadata">

**Author:** ![Spleeding1](https://forum.umbraco.com/user_avatar/forum.umbraco.com/spleeding1/32/1285_2.png) [@Spleeding1](https://forum.umbraco.com/u/Spleeding1)\
**Post date:** [September 5, 2025, 7:29pm UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/6 "2025-09-05T19:29:16Z")

</div>

I got this working and was trying to get it into a notification handler, but I am running into an issue of how to get the `contentTypeService` and `shortStringHelper` into the notification

```auto
public class DocumentTypeCreationHandler : INotificationHandler<SelectedNotification>
{
  public void Handle(SelectedNotification notification)
  {
    DocumentTypeCreation documentTypeCreation = new DocumentTypeCreation(
      {need contentTypeService and shortStringHelper here} );
    documentTypeCreation.CreateDocType();
  }
}

```

---

<div class="post-metadata">

**Author:** ![mistyn8](https://forum.umbraco.com/user_avatar/forum.umbraco.com/mistyn8/32/671_2.png) [@mistyn8](https://forum.umbraco.com/u/mistyn8)\
**Post date:** [September 6, 2025, 7:12pm UTC](https://forum.umbraco.com/t/create-document-type-programmatically/5702/7 "2025-09-06T19:12:59Z")

</div>

Something like?

```csharp
public class DocumentTypeCreationHandler : INotificationHandler<SelectedNotification>
{
    private readonly IContentTypeService _contentTypeService;
    private readonly IShortStringHelper _shortStringHelper;

    public DocumentTypeCreationHandler(IContentTypeService contentTypeService, IShortStringHelper shortStringHelper)
    {
        _contentTypeService = contentTypeService;
        _shortStringHelper = shortStringHelper;
    }

    public void Handle(SelectedNotification notification)
    {
        DocumentTypeCreation documentTypeCreation = new DocumentTypeCreation(
          _contentTypeService, _shortStringHelper );
        documentTypeCreation.CreateDocType();
    }
}

```
