The following is now of no use! The Campaign module in Sitefinity does not implement the IDynamicFieldContainer interface. This means that the Campaign type will not allow you to set custom fields. To resolve this, I created a link table in the SQL DB. I will write another post on how to achieve recurring newsletters in Sitefinity (until the team implements the required interface and the following solution will once again be relevant).
(mind you that although I’ve now crossed out the text, the code displayed is still relevant for other types that do implement the required interface. After adding both the type and the field in the manager, you can get or set the value from/to the field by simply typeInstance.GetValue(“customFieldName”))
Sometimes, you may want to add a custom field to one of the existing Sitefinity modules to extend functionality, or store more information than the OOTB offering. In my case, I wanted to extend the built-in Newsletters/Campaigns (Telerik.Sitefinity.Newsletters.Model.Campaign)to support recurring issues. The idea is that an issue is created once and then published in predefined time periods.
This will involve the use of a 3rd party job scheduler (for the time being, my best candidate is Quartz.NET), but haven’t reached that far yet. The reason for this post is that while working with the custom fields I needed to add to the Campaign object, I ran into a silly issue that I shouldn’t have encountered in the first place.
The logic in working with existing modules and custom fields is as follows:
1. grab a reference to the meta manager (handles fields)
var metaManager = Telerik.Sitefinity.Data.Metadata.MetadataManager.GetManager();
2. create the MetaType in the MetadataManager if it doesn’t exist
if (metaManager.GetMetaType(typeof(Telerik.Sitefinity.Newsletters.Model.Campaign)) == null) metaManager.CreateMetaType(typeof(Telerik.Sitefinity.Newsletters.Model.Campaign));
3. create the custom field
var myField = metaManager.CreateMetafield(“ScheduleTwo”);
metaManager.GetMetaType(typeof(Telerik.Sitefinity.Newsletters.Model.Campaign)).Fields.Add(myField);
4. apply changes
metaManager.SaveChanges();
Straightforward as this seems, there were a couple of issues.
1. The system does not check whether the “ScheduleTwo” MetaField already exists. It also doesn’t care if it does exist. It will keep adding a new “ScheduleTwo” MetaField each time the code is executed.
2. Executing the same .SaveChanges() method after deleting an existing custom MetaField, throws an “object reference not set to an instance of an object” error.
The following is my code to delete the lot of added custom MetaFields.
if (metaManager.GetMetafields().Where(f => f.FieldName == “ScheduleTwo”) != null)
{
foreach (var metafield in metaManager.GetMetafields().Where(f => f.FieldName == “ScheduleTwo”))
{
metaManager.Delete(metafield);
metaManager.SaveChanges(true); //by setting the flag to true, the schema is updated at the time of applying the changes and the aforementioned error is avoided.
}
}
The following using statements need to be added to the page:
using Telerik.Sitefinity;
using Telerik.Sitefinity.Newsletters.Model;
using Telerik.Sitefinity.Modules.Newsletters;
using Telerik.Sitefinity.DynamicModules.Builder;
Hopefully, I’ll come back to add the entire solution that takes care of scheduling recurring newsletters once that’s done. To be continued…