GetValidatedString() is not called for custom field type?

I was developing a custom field type that inherits from SPFieldUrl. I overrided the GetValidatedString(object value) method to perform the validation when the field was set to be mandatory. My overriden method however was not called by SharePoint, and hence the required field validation failed.

It turns out that it was because I was returning null in my field control class when a value was not specified for the field:

public override object Value
{
	get
	{
		EnsureChildControls();

		if (String.IsNullOrEmpty(txtUrl.Value))
		{
			return null;
		}

	        //...
	}
	//...
}

It appears that SharePoint does not call GetValidatedString() when the value is null. I changed the above code to below and everything works as expected:

if (String.IsNullOrEmpty(txtUrl.Value))
{
	return new SPFieldUrlValue();
}

In my next post I will provide a walkthrough of developing a custom field type that allows users to intuitively browse, upload and display an image to a SharePoint list. Watch this space :).

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Custom Field Types, SharePoint 2010. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s