In this tutorial, you will learn how to fix
Unable To Publish - String Or Binary Data Would Be Truncated On Saving errors. This error can occur when you are trying to create a new page or update an existing page in the CMS using code. The cause for this error can be the result of a classic developer schoolboy error 🤪.
The last time I encountered this error was at the end of a project during the content migration phase. I needed to import content from a legacy system into the new Umbraco CMS. I was using a code-first document-type creation architecture, using uSIteBuilder. While importing content, when the code tried to save the page this error was thrown annoying truncation error was thrown!
If you encounter this error while importing content and you look into the Umbraco database, you'll find a table called
CMSPropertyData is the table that will store all your properties data, as can be seen below:
As you can from the image above, the
dataNvarchar field is set to 500 characters max. If you want to store data in a property that uses
dataNvarchar you will be limited to 500 characters. The Umbraco
textstring data-type is stored in SQL as type
dataNvarchar. The truncation error means that you are trying a property that has too many characters when saving. From my experience, this property is usually the
name. When creating pages in the CMS, Umbraco provides validation that prevents this situation from occurring. When doing it in code, you just get an error!
If you get the error on the default 'name' property then the easiest option is to truncate the data before you save it. If the error happens on a custom document-type property and you cannot truncate your data, I suggest you change the type of property you are using on your document type. You need to use a property that uses
dataNvarchar in the background instead. Use a
textarea instead of a
If you want to go a more hacky route, you could run a SQL script to enlarge
dataNvarcharto use an
nvarchar(MAX) instead. This can be done, like so:
⚠️ DO NOT USE THIS ⚠️⚠️ If you Google this topic, you may see other tutorials that recommend running this script. I've been working with CMS systems for over a decade and one of my main tips is to leave the CMS database alone. Never try to modify it, there is never a good reason. It can always be avoided. Changing the database will just mean upgrade pains and bugs later on 😠😠😠
When you work with Umbraco you should only access data via an API, never directly in SQL. In this instance, it's much better to change your property type to use a
RichtextEditor instead. When you try and store data using the
RichtextEditor property, your data will be stored within the 'dataNText' column in the Umbraco database and you can save without any annoying errors!
If you are working on a legacy Umbraco project and you can't simply just delete the old property, you can write a script to map data store in one property into a newer one that does use the correct data type. The code to do this is pretty simple and would look something like this:
You are now a truncation fixing ninja 🐱👤! Happy Coding 🤘