0

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.

1 Answer 1

2

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.

5
  • Yes, I did use your solution from the other post. I also happen to be the same poster of that question as well as well as accepting your answer to that question! The details you provided elucidate why the settings gets truncated, so I really appreciate that. I am wondering how to re-enable some of those settings, i.e. say I want to show attachment folders... would I need to enable that through code?
    – Web User
    Commented Jan 23, 2017 at 18:24
  • 1
    As soon as you register your email ER on the doc lib, your code is in charge of everything. So, it's up to you to create folders for attachments, etc. SharePoint is not concerned anymore and won't manage parameters for something it's not concerned with.
    – Evariste
    Commented Jan 23, 2017 at 18:46
  • I am revisiting this topic again because I really need it to work. I have two environments, one based on MOSS 2007 and Exchange 2010, and another based on MOSS 2007 and Exchange 2013. I can email enable the list in the first environment and enable the handler and everything works as expected (even if the settings page is truncated). In the second environment, I have replicated the same set of configurations as in the first. Yet, I am unable to get the email enabled library to work. I really need to fix the issue in the second environment. Please share your thoughts if this sounds familiar.
    – Web User
    Commented Jun 13, 2017 at 9:16
  • 1
    When you say everything works as expected (even if the settings page is truncated): this is the question you had initially, and we already answered that: the fact that the "settings page is truncated" is perfectly expected and raisonnable: the hidden section is not needed when you have an external email ER. As for your second environment, this should be a new question, as it's not related to "Incoming Email Settings of Library truncated" question (with details on the problem).
    – Evariste
    Commented Jun 13, 2017 at 9:32
  • You are absolutely right, the problem with the second environment is best described in a new question, even if there are similarities. Thanks for suggesting it.
    – Web User
    Commented Jun 13, 2017 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.