Removing empty fields from EPiServer XForm e-mails

A customer asked me to see what I could do for their XForm order notification e-mails today. Apparently the department handling their incoming orders was receiving really long e-mails with loads of superfluous information; this as every field in the XForm was being submitted to them, and not just the ones containing data. This is the default behaviour of the XForm functionality, which of course can be changed with a short bit of code.

The only thing that one has to do is attach a short event handler to the BeforeSubmitPostedData event, and then let it filter out all of the unwanted data before allowing everything to proceed as usual.

The SaveFormDataEventArgs object contains the information being submitted by the user. Since we cannot loop through it and remove items from it at the same time, we first need to create a clone of the SerializableXmlDocument contained within (26). We will then use the copy for looping, while at the same time removing the unwanted XmlNodes from the original SerializableXmlDocument.

The method RemoveEmptyNodes(..) above recursively moves through every node in the clone, looking for those without content. When it finds one, it passes the name of this node to another recursive method; RemoveNodeByName(..). Even though the names, attributes and values of the nodes in the two SerializableXmlDocuments are identical, they are still completely different objects; hence, we cannot feed an XmlNode from the clone into the original and expect it to be found. So, we look for it using the node name.

The two methods are rather similar, but the latter one does the actual removing. It loops through every element until it finds the one with the proper name, and then deletes it. When this is done, the looping will stop and we return to the first method. It is not a problem using the name to find the correct XmlNode as the EPiServer XForm does not allow web editors to give multiple fields the same name within the same XForm.

File dumps of the SerializableXmlDocuments

I was curious to get a better look at what kind of XML that the XForm actually generated, so I did a quick save to a file. This is an example of what it looked like before I added my event handler:

Afterwards, only the relevant nodes were still there. The attributes are used for keeping track of what kind of value to expect from the field. It always seems to be this two level hierarchy in the XML, so I probably could have just retrieved the instance node by name, and then looped through it’s children; but where is the fun in that?

When the extra event handler is done removing XmlNodes from the original e.FormData.Data, EPiServer takes over and handles the rest. As the unwanted elements are no longer present in the SaveFormDataEventArgs, they will not be included in the mail.