I developed a feature receiver that attaches an email event receiver to an existing document library. Before activating the feature, the Incoming Email Settings page for the library looked like this:
The code to attach the email event receiver to the library is as follows:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
object webObj = properties.Feature.Parent;
if (webObj is SPWeb)
{
using (SPWeb web = webObj as SPWeb)
{
SPList incomingEmailList = web.Lists["IncomingEmailList"];
try
{
EnsureEmailER<IncomingEmailEventHandler>(incomingEmailList, "IncomingEmail_EmailAdded", SPEventReceiverType.EmailReceived);
}
catch (Exception e)
{
}
}
}
}
public void EnsureEmailER<T>(SPList list, string name, SPEventReceiverType type) where T : SPEmailEventReceiver
{
// We get the assembly/class names from the T generic type
string assemblyName = typeof(T).Assembly.FullName;
string className = typeof(T).FullName;
// We first remove any existing def matching the same criterias
List<SPEventReceiverDefinition> defToRemove = new List<SPEventReceiverDefinition>();
SPEventReceiverDefinitionCollection erCollection = list.EventReceivers;
foreach (SPEventReceiverDefinition def in erCollection)
{
if ((def.Class == className) && (def.Assembly == assemblyName) && (def.Type == type))
{
defToRemove.Add(def);
}
}
foreach (SPEventReceiverDefinition def in defToRemove)
{
def.Delete();
}
SPEventReceiverDefinition newDef = erCollection.Add();
newDef.Assembly = assemblyName;
newDef.Class = className;
newDef.Name = name;
newDef.Type = type;
newDef.SequenceNumber = 1000;
newDef.Update();
}
After activating the feature, the email event receiver works as expected. But the Incoming Email Settings is truncated and only the first section is available to modify. All other sections are gone!
Why does this happen? The environment I am working with is SharePoint 2007 and Visual Studio 2008.