Skip to content

Templating: Creating a doctype with template now yields a strongly typed template (closes #20443)#20688

Merged
AndyButland merged 6 commits intomainfrom
v17/bugfix/creating-a-doctype-with-template-does-not-yield-a-strongly-typed-template
Dec 10, 2025
Merged

Templating: Creating a doctype with template now yields a strongly typed template (closes #20443)#20688
AndyButland merged 6 commits intomainfrom
v17/bugfix/creating-a-doctype-with-template-does-not-yield-a-strongly-typed-template

Conversation

@lauraneto
Copy link
Contributor

Fixes #20443

In recent versions the flow of requests from the client when creating a document type with a template was:

  1. Create a template.
  2. Create a document type with the template id returned in 1.

In 1., the document type still didn't exist, so a "generic" template was being created.
Additionally, if the creation of the document type failed, the "orphan" template would remain.
This PR adds a new endpoint to the Management API:

POST /umbraco/management/api/v1/document-type/{guid}/template
{
  "alias": "templateAlias",
  "name": "templateName",
  "isDefault": false // Whether to set this template as the default for the document type
}

With the above in place, I adjusted the flow to:

  1. Create document type (with no template)
  2. Create template using the new endpoint (which will assign the template as default as well)

This way, a template is only created if the content type was actually created, and the generated template will know which model to reference.

The idea while building the new endpoint was also that you can also create templates for document types later on, default or not.

Testing
Create a Document Type with Template and verify that the generated template references the typed model and looks the same as in V13.
Also, ensure that creating templates through the backoffice still works as expected.

…ted for a content type

Add id, name and alias to the request payload to allow creating multiple templates for the same document type

Small adjustments

Remove unused import and unnecessary async

Switched content type template creation to content type controller

Missing constant export

# Conflicts:
#	src/Umbraco.Web.UI.Client/src/packages/core/backend-api/sdk.gen.ts
@lauraneto lauraneto changed the title Adjust the document type creation flow so that a template can be created for a content type Adjust the document type creation flow so that a template can be created for a content type (closes #20443) Oct 30, 2025
…does-not-yield-a-strongly-typed-template

# Conflicts:
#	src/Umbraco.Web.UI.Client/src/packages/core/backend-api/sdk.gen.ts
@lauraneto lauraneto marked this pull request as ready for review November 25, 2025 14:26
Copilot AI review requested due to automatic review settings November 25, 2025 14:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the document type creation flow to prevent orphaned templates. Previously, templates were created before document types, which could leave orphaned templates if the document type creation failed. The new approach creates the document type first, then creates the template via a new Management API endpoint (POST /umbraco/management/api/v1/document-type/{id}/template). This ensures templates are only created for successfully persisted document types and allows the server to generate templates with the correct model reference.

Key changes:

  • Introduces a new API endpoint for creating templates linked to specific document types
  • Moves template creation logic to occur after document type creation
  • Adds CreateTemplateAsync method to IContentTypeService and ITemplateService

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
document-type-workspace.context.ts Refactored _create to call template creation after document type creation; replaced direct template repository usage with document type template repository
document-type-template.repository.ts New repository for managing document type-specific template operations
document-type-template.server.data-source.ts New data source that calls the new Management API endpoint
types.ts Added UmbDocumentTypeTemplateModel type definition
TemplateService.cs Added new overload for CreateForContentTypeAsync accepting name, alias, and content type alias parameters
ContentTypeService.cs Added CreateTemplateAsync method that creates a template and assigns it to a content type
CreateDocumentTypeTemplateController.cs New API controller implementing the template creation endpoint
CreateDocumentTypeTemplateRequestModel.cs Request model for the new endpoint
content-type-workspace-context-base.ts Added reload() method to refresh workspace data
content-type-structure-manager.class.ts Added reload() method to fetch latest content type data

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@lauraneto lauraneto changed the title Adjust the document type creation flow so that a template can be created for a content type (closes #20443) Templating: Creating a doctype with template now yields a strongly typed template (closes #20443) Nov 25, 2025
Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me @lauraneto, and tests out as expected. Just a couple of inline comments to consider, and a response to your question.

Just wonder if you might look at ContentTypeServiceTests and see if it's worth adding a test or two to cover the CreateTemplateAsync method you have added, and similar for CreateForContentTypeAsync added to TemplateService.

lauraneto and others added 3 commits November 27, 2025 13:12
- Add integration tests for ContentTypeService.CreateTemplateAsync:
  - Success case with template association
  - NotFound status for non-existent content type
  - InvalidTemplateAlias for empty and too-long aliases
  - Default template assignment verification

- Fix bug in TemplateService.CreateAsync where alias validation
  occurred after GetViewContent call, causing ArgumentNullException
  for invalid aliases instead of returning InvalidAlias status

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional tests and other small updates all look good, approving and merging this now.

@AndyButland AndyButland merged commit 7b13e23 into main Dec 10, 2025
27 of 28 checks passed
@AndyButland AndyButland deleted the v17/bugfix/creating-a-doctype-with-template-does-not-yield-a-strongly-typed-template branch December 10, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating a doctype with template does not yield a strongly typed templated

2 participants