SharePoint 2007 (MOSS/WSS) - Programmatically modifying the incoming email setting for Document Libraries

Requirement: I want to access and modify all the options given in Incoming Email setting page of Document Library programmatically.

 

pic

 

Here is the code of a Web Part for the same:

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace WPDocLibEmailSetting

{

[Guid("b5ddfbff-afbb-42bb-b4ea-3e3788ad0297")]

public class WPDocLibEmailSetting : System.Web.UI.WebControls.WebParts.WebPart

{

TextBox myText;

Button myBtn;

public WPDocLibEmailSetting()

{

}

protected override void CreateChildControls()

{

base.CreateChildControls();

myText = new TextBox();

myBtn = new Button();

myBtn.Text = "Submit";

myBtn.Click += new EventHandler(myBtn_Click);

this.Controls.Add(myText);

this.Controls.Add(myBtn);

}

void myBtn_Click(object sender, EventArgs e)

{

SPWeb site = SPContext.Current.Web;

Guid newListGuid = site.Lists.Add(myText.Text, myText.Text, SPListTemplateType.DocumentLibrary);

SPList newList = site.Lists[newListGuid];

newList.ContentTypesEnabled = true;

newList.OnQuickLaunch = true;

newList.EnableAssignToEmail = true;

newList.EmailAlias = myText.Text;

newList.Update();

newList.RootFolder.Properties["vti_emailattachmentfolders"] = "sender";

//Group attachments in folders, options: "subject"/"sender"/"root"

newList.RootFolder.Properties["vti_emailoverwrite"] = 0;

//Overwrite files with the same name, options 1/0

newList.RootFolder.Properties["vti_emailsaveoriginal"] = 1;

//Save original e-mail, options 1/0

newList.RootFolder.Properties["vti_emailsavemeetings"] = 0;

//Save meeting invitations, options 1/0

newList.RootFolder.Properties["vti_emailusesecurity"] = 1;

//Email Security Policy, options 1/0

newList.RootFolder.Update();

}

protected override void Render(HtmlTextWriter writer)

{

//base.Render(writer);

EnsureChildControls();

myText.RenderControl(writer);

myBtn.RenderControl(writer);

}

}

}

Comments

  • Anonymous
    April 01, 2008
    PingBack from http://stevepietrek.com/2008/04/01/links-412008/

  • Anonymous
    June 18, 2008
    A call to Update is resulting in a very opaque error being thrown: Microsoft.SharePoint.SPException: Error in the application.   at Microsoft.SharePoint.SPList.UpdateDirectoryManagementService(String oldAlias, String newAlias)   at Microsoft.SharePoint.SPList.Update(Boolean bFromMigration)   at Microsoft.SharePoint.SPList.Update() Any thoughts? Write-Host "Enabling Email Archiving"; $DiscussionsList = $ClientWeb.Lists["Discussions"]; $DiscussionsList.EmailAlias = $LowerDomainNameWithoutSuffix + "-archive"; $DiscussionsList.ContentTypesEnabled = $true; $DiscussionsList.Update(); $RootFolder = $DiscussionsList.RootFolder; $RootFolder.Properties["vti_emailsaveattachments"] = 1; $RootFolder.Properties["vti_emailsavemeetings"] = 0; $RootFolder.Properties["vti_emailsaveoriginal"] = 0; $RootFolder.Properties["vti_emailusesecurity"] = 0; $RootFolder.Update();

  • Anonymous
    July 27, 2008
    I have the same problem. Whenever I try to set the EmailAlias property and then to update() the list, the update failes and results with the following exception: Microsoft.SharePoint.SPException: Error in the Application.  at Microsoft.SharePoint.SPList.UpdateDirectoryManagementService(String oldAlias, String newAlias)  at Microsoft.SharePoint.SPList.Update(Boolean bFromMigration)  at Microsoft.SharePoint.SPList.Update() I'm using the same code as the above: newList.EmailAlias = "test"; newList.Update(); Does anyone have an idea what is the reason for this? Thanks.

  • Anonymous
    August 13, 2008
    I had the same problem (Application Error). It turned out that I was using the administrator account to launch my script and this account had not the appropriate rights on the portal. Once I opened my power shell console with an account authorized on the portal, the script worked.

  • Anonymous
    April 21, 2009
    Which  are those "appropiate rights on the portal"? Thanks in advance! jm.sanagerico@layetana.com

  • Anonymous
    December 02, 2009
    Pretty good article. I found it really helpful. I would like to add a little comment: //Email Security Policy, options 1/0 newList.RootFolder.Properties["vti_emailusesecurity"] = 1; 1 means "Accept emails based on permissions" 0 means "Accept emails from any sender" Thank very much man. Carlos Vigo (carlosvigopaz@yahoo.com)

  • Anonymous
    April 21, 2010
    Thank you for this post! I have been trying to figure this out for hours. I am astonished that they didn't create methods for this. Seems like any setting that you can set through the UI should be easy to set through the API.

  • Anonymous
    May 10, 2010
    Hi, Can anyone please tell me the property to enable publishing the incoming email programmatically?