Mail enabling an SPList programmatically
I am currently building a tool which creates SharePoint sites automatically. Each site must have a mailbox enabled and a specific Email address specified.
I can create the site wh开发者_C百科ich by default has a SharePoint list named "mailbox".
My questions is how to I allow the "mailbox" SPList to receive email. There is a property within an SPList called CanReceiveEmail but this is Read-only? So is there another way?
I also need to be able to set the Email address of the SPList programtically, does anyone know the best way to do this?
It can be done manually but this is not an option.
A simple list (as far as I know but I may be wrong) does not have the ability to receive emails. You can create a custom list that does.
You can create a document library, which by default has this ability.
How these properties are set, you can see when you examine the code (using Lutz Roeder's Reflector, http://www.red-gate.com/products/reflector/) of /_layouts/EmailSettings.aspx
which can be found in "Microsoft.SharePoint.ApplicationPages.dll" found on your server in path something like
\\server\c$\Inetpub\wwwroot\wss\VirtualDirectories\80\_app_bin
.
So you will have to set multiple properties of the "rootfolder" of the document library.
The code is as follows :
Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal args As EventArgs)
If Me.EnabledTrue.Checked Then
If ((Me.TxtAlias.Text Is Nothing) OrElse (Me.TxtAlias.Text.Length = 0)) Then
Throw New SPException(SPResource.GetString("MissingEmailAlias", New Object(0 - 1) {}))
End If
'This will be the receiving e-mail address
Me.m_List.EmailAlias = Me.TxtAlias.Text
'do we need to check users permissions on items
Me.m_RootFolder.Properties.Item("vti_emailusesecurity") = IIf(Me.UseSecurityTrue.Checked, 1, 0)
If Me.ShowSaveAttachments Then
'options how to save attachments, root folder, grouped, whatever
Me.m_RootFolder.Properties.Item("vti_emailsaveattachments") = IIf(Me.SaveAttachmentsTrue.Checked, 1, 0)
End If
If Me.ShowSaveOriginalAndMeetings Then
Me.m_RootFolder.Properties.Item("vti_emailsavemeetings") = IIf(Me.MeetingsTrue.Checked, 1, 0)
Me.m_RootFolder.Properties.Item("vti_emailsaveoriginal") = IIf(Me.SaveOriginalTrue.Checked, 1, 0)
End If
If Me.ShowAttachmentFolders Then
Me.m_RootFolder.Properties.Item("vti_emailoverwrite") = IIf(Me.OverwriteTrue.Checked, 1, 0)
If Me.AttachmentFoldersSender.Checked Then
Me.m_RootFolder.Properties.Item("vti_emailattachmentfolders") = "sender"
ElseIf Me.AttachmentFoldersSubject.Checked Then
Me.m_RootFolder.Properties.Item("vti_emailattachmentfolders") = "subject"
Else
Me.m_RootFolder.Properties.Item("vti_emailattachmentfolders") = "root"
End If
End If
If Me.ShowAutoApprove Then
'I think this is something when content approval is enabled.
Me.m_RootFolder.Properties.Item("vti_emailautoapprove") = IIf(Me.AutoApproveTrue.Checked, 1, 0)
End If
ElseIf Me.EnabledFalse.Checked Then
Me.m_List.EmailAlias = Nothing
End If
Me.m_RootFolder.Update
Me.m_List.ResetContentTypes
Me.m_List.Update
SPUtility.Redirect((IIf((Me.m_List.BaseType = SPBaseType.Survey), "survedit", "listedit") & ".aspx?List=" & Me.m_List.ID.ToString), SPRedirectFlags.RelativeToLocalizedLayoutsPage, Me.Context)
End Sub
EDIT: added comments to my code
精彩评论