SharePoint Event Receiver ItemUpdating Event and Hidden Columns


I was recently developing a list item Event Receiver, implementing the ItemAdded and ItemUpdating events.

The purpose of this event receiver is to populate fields of items of a particular content type, and the fields in question are hidden from the user by setting the ShowInNewForm and ShowInEditForm properties of the  FieldRef elements when the fields are provisioned in a content type, as shown below.

<FieldRefs>
	<FieldRef ID="{8F134B57-1435-441b-A9F7-9A26BD7BCD43}" Name="HiddenField1" Required="FALSE" ShowInNewForm="FALSE" ShowInEditForm="FALSE" />
	<FieldRef ID="{3F526338-9B22-4bf2-8A76-36DF204D9EB0}" Name="HiddenField2" Required="FALSE" ShowInNewForm="FALSE" ShowInEditForm="FALSE" />
</FieldRefs>

After deploying the event receiver to a document library and testing it I noticed that when a document was added to the document library, the event receiver worked as it should, but when updating the document properties the changes made by the event receiver (ItemUpdating) were not persisted.

Initially I thought that I’d got my use of AfterProperties/BeforeProperties mixed up in the ItemUpdating event method, however after checking this (thanks to a great post by Alex), I was using AfterProperties correctly.

Knowing that I’d used a similar event receiver in another deployment without a problem I realised that the only difference between the two scenarios was that the fields I was trying to update were marked ShowInNewForm/ShowInEditForm as FALSE.

Trawling google showed some posts indicating that fields such as these didn’t appear in the AfterProperties collection, however the code I had would report this condition and it wasn’t the case – indeed stepping through the code revealed that these fields did exist in the AfterProperties collection, see code below.

public override void ItemUpdating(SPItemEventProperties properties)
{
	if (properties.ListItemId < 1) return;
	base.ItemUpdating(properties);
	try
	{
		DisableEventFiring();
		InitialiseReceiverData(properties);
		if (_registeredProperties.Count > 0)
		{
			foreach (var registeredProperty in _registeredProperties)
			{
				if (properties.AfterProperties[registeredProperty.Name] == null)
				{
					var msg =
						string.Format(
							"ItemUpdating: ListItem contenttype {0} does not include registered property column {1}",
							properties.AfterProperties["ContentType"],
							registeredProperty.Name);
					HandleWarning(msg, properties);
					continue;
				}
				properties.AfterProperties[registeredProperty.Name] = registeredProperty.Value;
			}
			HandleInformation(string.Format("Updated properties at {0}.ItemUpdating",
												GetType().Name), properties);
		}
	}
	catch (Exception ex)
	{
		Utility.HandleException(string.Format("Exception at {0}.ItemUpdating",
										GetType().Name), properties, ex);
	}
	EnableEventFiring();
}

I wrote a small console app to update these fields and set the ShowInNewForm/ShowInEditForm properties to TRUE, reran my test and it worked as expected.

In view of this I changed the way the fields were provisioned to make them Hidden = TRUE

<FieldRefs>
	<FieldRef ID="{8F134B57-1435-441b-A9F7-9A26BD7BCD43}" Name="HiddenField1" Required="FALSE" Hidden="TRUE" />
	<FieldRef ID="{3F526338-9B22-4bf2-8A76-36DF204D9EB0}" Name="HiddenField2" Required="FALSE" Hidden="TRUE" />
</FieldRefs>

Redeployed everything, tested, and the problem was resolved with the changes made by the event receivers ItemUpdating event being persisted correctly.

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

2 thoughts on “SharePoint Event Receiver ItemUpdating Event and Hidden Columns

  1. Nice post good to know information because we will face this same soon on one project.

    But this solution means that the fields are always hidden. Right?

    I mean that if I want to be able to show the field on ShowInViewForms this solution will prevent that also.

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