# Get all page templates in code

**URL:** <https://forum.litium.com/t/get-all-page-templates-in-code/259>\
**Category:** Questions\
**Created:** [September 28, 2018, 11:11am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259 "2018-09-28T11:11:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [September 28, 2018, 11:11am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/1 "2018-09-28T11:11:46Z")

</div>

I need to programmatically get hold of a page Template (CMS\_Template) by name. What is the best way to do this? Is there any way of getting all existing page Templates through code?

Litium version: 6.2.1

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [September 28, 2018, 11:20am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/2 "2018-09-28T11:20:37Z")

</div>

You can get template from its pagetype if that is known.

```auto
var pageType = ModuleCMS.Instance.PageTypes.GetPageType("MyPageType");
var template = pageType.Templates.GetTemplate("TemplateName");
```

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [September 28, 2018, 11:25am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/3 "2018-09-28T11:25:53Z")

</div>

The templates I need to handle aren’t connected to any page types (due to some unfortunate naming choices).

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [September 28, 2018, 11:28am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/4 "2018-09-28T11:28:24Z")

</div>

Templates in CMS are always connected to pagetypes? How can you have a disconnected template?

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [September 28, 2018, 11:38am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/5 "2018-09-28T11:38:04Z")

</div>

Legacy data. We have a lot of page types that were created long before MVC was a thing in Litium (created from BO and not from code). I’ve created PageTypeDefinitions and MVC controllers for them, and most page types are getting the correct template automatically through name matching. But we have some special cases where the old page template names don’t match the new PageTypeDefinition + Controller names. For example, I have 3 page types (with 3 different names obviously) that I need to connect to one MVC template with a completely different name.

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [September 28, 2018, 11:48am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/6 "2018-09-28T11:48:32Z")

</div>

Your old templates should still be connected to pagetypes, you can always check which one in the database.

A template is connected to a pagetype by decorating the controller as PageTemplate so Controller name should not be an issue: `[PageTemplate(typeof(Article), "~/Site/Images/Article.png", Name = "Article1")]`.

And if you need to connect multiple pagetypes to one controller you can do that in Litium backoffice, or create three controller actions that return the same view.

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [September 28, 2018, 11:54am UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/7 "2018-09-28T11:54:25Z")

</div>

I’ll try the multiple-controller-actions approach.

For future reference - is there any way of getting a list of all templates/getting a specific template by name without going through the page type?

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [September 28, 2018, 12:00pm UTC](https://forum.litium.com/t/get-all-page-templates-in-code/259/8 "2018-09-28T12:00:06Z")

</div>

Not that I know of since templates are only the display for pagetypes, but normally you know which pagetype you are working with, not really sure what your scenario is.

With Litium 7 this will be different and the templates will be as they are in Customers/PIM.
