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);
is a good try and you was only missing a small parts.
Try with the following instead.
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.