Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hitesh Sharma 24 posts 124 karma points
    Sep 14, 2018 @ 07:13
    Hitesh Sharma
    0

    Change Datatype without loosing data

    I have a checkbox list data type. Now I want to use a different checkbox list. How can I do that without loosing data? i.e. I want to map the values saved for my previous checkbox list with the new checkbox list.

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Sep 15, 2018 @ 10:11
    Marc Goodson
    101

    Hi Hitesh

    If your two Checkbox list data types store data in exactly the same format, then you will just be able to switch the property editor used for your data type, without losing any data. (create two properties, one of each type, pick some values in both, then switch the properties to use textarea, this will show the formats of how the data that is stored in each property editor)

    However if they store in different formats, then you would need to convert the old data format into the new...

    ... now depending on how many instances of the Checkbox list you have on various pages in your site, depends on the best strategy to take:

    eg if you have 5 pages, then it will be quicker to just make a note of the settings, make the switch and repick them - (you can switch to an interim text area property editor first to alter the format of how the values are stored to the new format)

    if you need to automate the change, then there are a couple of strategies:

    1) Make the change when the editor loads the page in the Umbraco backoffice.

    You can achieve this by capturing Umbraco's SendingContentModel event, essentially this fires just before a page is loaded into the Umbraco backoffice: see https://our.umbraco.com/documentation/reference/events/EditorModel-Events

    You have access to the stored data, via e.Model.Properties, and you can manipulate the value stored for a property before it is loaded for the Editor to see, and it's here you would write the code to migrate the values from the old format to the new. To the editor they will just see the values in the new property editor's checkbox list, when they republish the page, the format will be updated to the database.

    2) Allow the editor to control when each page changes it's checkboxlist

    In some circumstances it might be best to allow the editor to control, when these values change, to do this you would create a new document type, with your updated checkbox list property editor, and allow the editor to switch the content to use the new document type using the 'Change Document Type' option, you would then write code to intercept this change and update the stored format of the checkbox list value: there is an article here on Skrift that talks about this approach: https://skrift.io/articles/archive/enriching-your-editors-by-converting-properties-on-doctype-change/

    3) Loop through all your content, and use the ContentService, to update the values to the new format:

    https://our.umbraco.com/documentation/reference/management/services/contentservice

    (this can be slow if you have tons of content)

    4) and finally the scary faster approach - Update directly the Umbraco database to change one format into another

    This isn't for the feint hearted, and so you can properly muck things up this way, always take a database backup before taking this kind of approach.

    Umbraco stores all property data in the table cmsPropertyData, joined to a cmsPropertyType table, so you can find all rows of data for your dataTypeName via the following SQL

    Select *
                FROM cmsPropertyType cpt 
                INNER JOIN umbracoNode ubn on ubn.id = cpt.datatypeId INNER JOIN cmsDataType cdt On ubn.id = cdt.nodeid
                INNER JOIN cmsPropertyData cpd ON cpd.propertyTypeId = cpt.Id
                WHERE [text] = 'dataTypeName'
    

    you 'could' then update the values stored directly in the db.

    You would also need to republish any pages that had been updated to trigger the update in the umbraco cache file, and also the cmsXMLContent database table.

    So hopefully they both just store in the same format and you don't need to do anything scary!

    regards

    Marc

  • Hitesh Sharma 24 posts 124 karma points
    Sep 17, 2018 @ 04:49
    Hitesh Sharma
    0

    Hi Marc,

    I must have to say, "You Rock". I've never saw such detailed explanation. You saved my week. Thanks a ton!

    Regards, Hitesh

Please Sign in or register to post replies

Write your reply to:

Draft