# How to add fields to an existing page field template?

**URL:** https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236
**Category:** Questions
**Tags:** litium-7
**Created:** [November 29, 2019, 11:34am UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236 "2019-11-29T11:34:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![amazedsaint](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@amazedsaint](https://forum.litium.com/u/amazedsaint)
#### Post date: [November 29, 2019, 11:34am UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236/1 "2019-11-29T11:34:50Z")

</div>

We have recently upgraded to Litium 7 from version 6.x. And we havn’t created definition classes for the existing page templates. Now we have a requirement to add some fields to one of the page templates(through code). I was able to create field definition for the fields, but i don’t know how to add them to the template through code.

All the samples i saw expects me to create definition classes for the  
existing page templates.

Is there a way to add fields to a page template through code without creating page template classes?

Something like

`var sampleTemplate = IoC.Resolve<FieldTemplateService>().Get<PageFieldTemplate>(TemplateNameConstants.SampleTemplateName);`

`sampleTemplate.FieldGroups.FirstOrDefault(fg=>fg.Id=="General").Fields.Add(FieldNameConstants.SampleFieldName);`

Litium version: 7.2.3

---

<div class="post-metadata">

### Author: ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)
#### Post date: [November 29, 2019, 1:18pm UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236/2 "2019-11-29T13:18:34Z")

</div>

Take a look in the files in folder:

> Litium.Accelerator.Definitions.Pages

You will then get some hints.

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [December 2, 2019, 4:05pm UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236/3 "2019-12-02T16:05:48Z")

</div>

> [@amazedsaint](#):
>
> Something like
> 
> `var sampleTemplate = IoC.Resolve<FieldTemplateService>().Get<PageFieldTemplate>(TemplateNameConstants.SampleTemplateName);`
> 
> `sampleTemplate.FieldGroups.FirstOrDefault(fg=>fg.Id=="General").Fields.Add(FieldNameConstants.SampleFieldName);`

is a good try and you was only missing a small parts.

Try with the following instead.

```auto
var templateService = IoC.Resolve<FieldTemplateService>(); // Inject the FieldTemplateService in the class instead of using IoC.Resolve.
var sampleTemplate = templateService.Get<PageFieldTemplate>(TemplateNameConstants.SampleTemplateName)?.MakeWritableClone();` // Make writable clone of the template, otherwise the template is read only.

sampleTemplate.FieldGroups.FirstOrDefault(fg=>fg.Id=="General").Fields.Add(FieldNameConstants.SampleFieldName);

templateService.Update(sampleTemplate); // Don't forget to persist the changes.

```

---

<div class="post-metadata">

### Author: ![amazedsaint](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@amazedsaint](https://forum.litium.com/u/amazedsaint)
#### Post date: [December 17, 2019, 7:25am UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236/4 "2019-12-17T07:25:07Z")

</div>

Thank you so much, it worked.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)
#### Post date: [January 14, 2020, 7:25am UTC](https://forum.litium.com/t/how-to-add-fields-to-an-existing-page-field-template/1236/5 "2020-01-14T07:25:08Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
