Search code examples
c#umbracoumbraco7

Umbraco - How do I access a Content Blueprint (template) in code


So I know that I am able to obtain a content node with the following code:

Node contentNode = new Node(contentId);

I would like to access a content blueprint from within my code in a similar fashion, how would I go about this?


Solution

  • I clearly didn't understand what a content template was, so thank you Claus for explaining this to me.

    I did eventually find the solution to my problem. It turns out that content templates are also referred to as blueprints. You can obtain the content template (AKA blueprint) with the following code:

    ApplicationContext applicationContext = ApplicationContext.Current;
    
    // Obtain the Umbraco Content Service
    var contentService = applicationContext.Services.ContentService;
    
    // Obtain content template
    var contentTemplate = contentService.GetBlueprintById(contentTemplateId);
    

    If you then wanted to create a content node from this blueprint you could use:

    // Create content node from content template
    var content = contentService.CreateContentFromBlueprint(contentTemplate, "Content Node Name");
    
    // Save and publish to new content node
    Attempt<Umbraco.Core.Publishing.PublishStatus> publishStatus = contentService.SaveAndPublishWithStatus(content);