Change existing field type in module from short to long text
I have a field that initially worked as a short text field but needs to be changed to a long text field to accommodate bigger values. I have tried changing the character length in the database but it didn't work (see attached file). Do I need to delete the existing field and re-add it as a long text type? And how do I do that without losing all my data? Any help would be appreciated. Thanks.
Hello Louis,
You cannot change the type of an existing field, however you can move its value to a newly created LongText field and then delete the old field.
The way to do this is by first creating a new field of type Long text. After that you can run the following code which will set the data of the long text field to be the same as the data of the short text field.
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
// Resolve dynamic content item types (plural name in yellow, singular name in red)
Type magazineType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Magazines.Magazine"
);
var allMagazines = dynamicModuleManager.GetDataItems(magazineType).Where(i => i.Status == ContentLifecycleStatus.Master);
foreach
(var item
in
allMagazines)
item.SetValue(
"LongField"
, item.GetValue(
"ShortField"
));
dynamicModuleManager.SaveChanges();
dynamicModuleManager.Lifecycle.Publish(item);
using
System;
using
System.Linq;
using
Telerik.Sitefinity.DynamicModules;
using
Telerik.Sitefinity.GenericContent.Model;
using
Telerik.Sitefinity.Model;
using
Telerik.Sitefinity.Utilities.TypeConverters;