Change existing field type in module from short to long text - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Change existing field type in module from short to long text

  • Change existing field type in module from short to long text
  • b88f511e-37c1-6deb-a958-ff0000446526_screenshot-database.png
    ​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"));

    In order to use this code the following namespaces must be included:

    using System;
    using System.Linq;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Utilities.TypeConverters;

    The last step is to remove the Short text field from the module.

    Velizar Bishurov
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items