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 with0
.
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
-
Backup existing content
- Any schema change carries risk, so backup for disaster recovery.
-
Create a new
ShortText
field calledPostalCodes
ShortText
maps tonvarchar(255)
and supports leading zeros.
-
Rename the old
PostalCode
field toPostal Code (Old)
- To avoid confusion between the old and new fields.
-
Run the field migration tool
- See section below for how.
-
Verify migrated data
-
Rearrange UI components (dashboard, frontend, forms)
- For some reason, Sitefinityโs field arrangement may break on update.
-
Delete old
PostalCode
field once everything works.
๐ฅ Migration Tool
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