Question

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: Incoming Email Settings (Out Of The Box)

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! Incoming Email Settings (after email event receiver is attached via feature receiver

Why does this happen? The environment I am working with is SharePoint 2007 and Visual Studio 2008.

Était-ce utile?

La solution

This perfectly makes sense to me: as soon as you attach an email ER, your code is in charge of handling incoming emails for this doc lib, according to your own business rules.
So standard settings used by SharePoint when SharePoint does handle emails are not needed anymore in that case.
Your code will handle email management logic, with dedicated settings you are free to hard-code/store somewhere/etc.

[Note: glad to see you're reusing the code I had posted here Deploy/bind event receivers to lists in single feature! :)]

EDIT - what's under the hood
I dug into the code of the page. The page that displays the incoming email settings for the doc lib is "EmailSettings.aspx". If you open it, you see that the bottom sections are visible only under certain conditions, for instance:

<%
if (ShowAttachmentFolders)
{
%>
    <wssuc:InputFormSection Title="<%$Resources:wss,emailsettings_attachments_title%>" runat="server">
        ...
    </wssuc:InputFormSection>
<%
}
%>

So, the section shows up only if ShowAttachmentFolders is true. Looking at the code for ShowAttachmentFolders in ILSpy (class Microsoft.SharePoint.ApplicationPages.EmailSettingsPage, you see:

protected bool ShowSaveAttachments
{
    get
    {
        return !this.HasExternalHandler && (...);
    }
}

And HasExternalHandler is:

protected bool HasExternalHandler
{
    get
    {
        bool result = false;
        foreach (SPEventReceiverDefinition sPEventReceiverDefinition in this.m_List.EventReceivers)
        {
            if (sPEventReceiverDefinition.Type == SPEventReceiverType.EmailReceived)
            {
                result = true;
            }
        }
        return result;
    }
}

So, basically, this confirms the behavior is made on purpose by Microsoft, and that as soon as you have one ER of type EmailReceived registered on the list, most of the configuration sections do not appear on the page.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top