Event Receiver to Fill Title Field Automatically

In SharePoint, Title field cannot be deleted from any SharePoint document library or list. It is a single line of text field with 255 maximum characters. It can be hidden from SharePoint document library edit form, it can be renamed to any field name we like, but still it is there, can not be made gone forever.

On the other hand, sometimes our clients need a bigger field to store title or caption. Let’s say they need at least 500 characters for Title field. Of course we can create other field, a multiple line text field for this purpose. But we end up with two fields for title. We can let the original Title field hidden in any edit forms. But, this field is still referenced in other places, for example in search result page. We need to fill the Title field with something.

So, we think that a better approach is to fill the Title field automatically by taking the first 255 characters from the custom title field. And we can do it using event receiver. We can create ItemUpdated for this purpose. The idea is after updating an item, take the first 255 characters from custom title, and assign it to Title. Why ItemUpdated? not ItemAdded? That’s because in a document library we usually upload document first, then fill its properties. Filling the property after uploading the document is an update process. Needless to say that if you are working with a list, you should use ItemAdded event.

Here is the code snippet of the ItemUpdated event receiver:

/// <summary>
/// An item was updated
/// </summary>
public override void ItemUpdated(SPItemEventProperties properties)
	this.EventFiringEnabled = false;
	SPListItem thisItem = properties.ListItem;
	string customTitle = thisItem["CustomTitle"].ToString();
	if (customTitle.Length > 255)
		thisItem["Title"] = customTitle.Substring(0, 250) + "...";
		thisItem["Title"] = customTitle;

	this.EventFiringEnabled = true;

The source code needs no explanation, except for the line containing this.EventFiringEnabled = false. This line is necessary and important. It tells SharePoint not to fire update event when internally updating its Title field. If we don’t do this, another ItemUpdated event will be triggered when Title is updated. This event firing will happen indefinitely, and eventually it will throw error message. So, don’t forget to put that line, and the line to enable event firing again after calling thisItem.Update() method.

The following image shows the Title field is automatically filled from Judul Perundangan Hukum (our custom title) field:

Auto-filled Title

Source: Event Receiver to Fill Title Field Automatically


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s