Fixing Postal Code Type from Number to ShortText

sitefinity | 2021-06-20

๐Ÿšฉ Background Story

We had originally set up a content type in Sitefinity with PostalCode as a Number field. While this sounds fine at first, we later learned that:

  • In coding/Sitefinity, a Number field is actually mapped to a decimal column in SQL.
  • A decimal field cannot start with 0, which makes it unsuitable for Singapore postal codes, as some begin with 0.

This caused data entry issues. So, we needed to fix our mistake and allow valid postal codes, including those starting with 0.


โœ… Steps to Counter This Issue

  1. Backup existing content

    • Any schema change carries risk, so backup for disaster recovery.
  2. Create a new ShortText field called PostalCodes

    • ShortText maps to nvarchar(255) and supports leading zeros.
  3. Rename the old PostalCode field to Postal Code (Old)

    • To avoid confusion between the old and new fields.
  4. Run the field migration tool

    • See section below for how.
  5. Verify migrated data

  6. Rearrange UI components (dashboard, frontend, forms)

    • For some reason, Sitefinityโ€™s field arrangement may break on update.
  7. Delete old PostalCode field once everything works.


๐Ÿ”ฅ Migration Tool

screen1

We created a simple admin-only widget with:

  • A text input (for method name)
  • A submit button
  • A message area for result feedback

We used C# Reflection to dynamically call migration methods from a generic utility class. Hereโ€™s what happens:


Scenarios

โŒ Method name mismatch:
Returns:
โ€œ(method name) is an inappropriate method name.โ€

โš ๏ธ Method exists, but new field not created:
Returns:
โ€œPlease setup the new field and run the migration tool again.โ€

โœ… Method exists and field is ready:
Returns:
A list of migrated content items.

Just type in the method name, hit submit, and boom โ€” old postal codes are copied into the new field!

๐Ÿ’พ Outcomes

  • We learned and applied Reflection in real use case
  • Created a reusable tool for other field migrations

๐Ÿ“Œ Further Enhancements

  • Allow passing of content type, source field, and destination field via HTTP POST
  • Generalize the migration logic to be fully dynamic

๐Ÿ”— Code Snippets

๐Ÿ“š References