Copied to clipboard

Flag this post as spam?

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


  • Jan Højriis Dragsbæk 12 posts 63 karma points
    Apr 19, 2013 @ 10:17
    Jan Højriis Dragsbæk
    0

    Programmatically setting a template on a document in V6 API

    I am currently working on a product importer, where I have to decide dynamically which template I have to assign to a product based on where it is in the umbraco tree. The document type currently has two different templates i have to change between - lets call them A and B. 

    I can see in the API than when i use the method GetById from ContentService, that I have access to a property named Template that takes an ITemplate.

    Is there a way to programmatically pull out a list of available templates for the document and set the template based on that list, or am I forced to hardcode the templates?

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 19, 2013 @ 11:28
    Morten Christensen
    101

    If you want to get allowed templates for a content item you have to go through the ContentType, so on your Content object you have the following:

    content.ContentType.AllowedTemplates and the default template will be content.ContentType.DefaultTemplate, which usually corresponds to content.Template unless its been set to one of the other AllowedTemplates of course.

    So in order to update the list of AllowedTemplates on a ContentType or change a Template on the Content object you can fetch templates through the FileService:
    fileService.GetTemplates(); <- this method also allows you to pass in an array of aliases. But by default it'll retrieve all masterpages in the masterpages-folder or view in the Views-folder (one thing to note is that it currently only looks in the root of one of these two folders).
    fileService.GetTemplate("myTemplateAlias"); 

    So having found the correct Template object you can create a new list of AllowedTemplates or add a new DefaultTemplate and save the ContentType through the ContentTypeService or you can set the Template directly on the Content object and then save that through the ContentService.

    Hope this helps.

    - Morten

Please Sign in or register to post replies

Write your reply to:

Draft