在本文中,将探讨如何通过自定义Outlook的RibbonX来实现邮件附件直接上传到SharePoint的功能。这种集成可以极大地提高工作效率,尤其是在处理大量邮件和附件时。
首先,需要确保已经安装了SharePointFoundation客户端对象模型DLL。这可以通过以下链接下载:
下载完成后,将DLL文件放置在以下路径:
C:\Program Files\Common Files\microsoft shared\SharePointClient\Microsoft.SharePoint.Client.dll
接下来,需要下载并开始使用Microsoft提供的Ribbon示例,这将帮助轻松地为邮件项添加右键事件。示例可以从以下链接获取:
为了启用右键事件,需要修改explorer.xml文件。以下是修改后的代码示例:
<contextMenus>
<contextMenu idMso="ContextMenuMailItem">
<menuSeparator id="MySeparator" />
<menu id="MySubMenu" label="submenu">
<button id="MyContextMenuMailItem4" label="Attachment to Sharepoint" onAction="LaunchAttachUp" />
<button id="MyContextMenuMailItem5" label="Meeting Invite" onAction="CreateMeeting" />
</menu>
</contextMenu>
</contextMenus>
在这个示例中,添加了一个名为"Attachment to Sharepoint"的按钮,当用户在邮件上点击右键时,这个按钮将会出现。
接下来,需要在RibbonXAddin.cs文件中添加LaunchAttachUp方法。这个方法将验证上下文,并调用上传到SharePoint的函数。以下是实现代码:
public void LaunchAttachUp(Office.IRibbonControl control)
{
if (control.Context is Outlook.Selection)
{
Outlook.Selection selection = control.Context as Outlook.Selection;
if (selection.Count == 1)
{
if (selection[1] is Outlook.MailItem)
{
Outlook.MailItem oMail = selection[1] as Outlook.MailItem;
ShareAttach sa = new ShareAttach();
sa.AttachmentSharepoint(oMail, "http://SITEURLFORSHAREPOINT.COM", "/sites/YOURSITENAME/Shared Documents/");
}
}
}
}
在这段代码中,首先检查当前的上下文是否为Outlook.Selection,然后检查选择的邮件项是否为Outlook.MailItem。如果是,就创建一个ShareAttach对象,并调用其AttachmentSharepoint方法来上传附件。
AttachmentSharepoint方法会遍历邮件的附件,并根据附件类型进行处理。以下是处理附件的代码示例:
public void AttachmentSharepoint(Outlook.MailItem mailItem, string clientContextURL, string sitelibfolder)
{
if (mailItem != null)
{
var attachments = mailItem.Attachments;
foreach (Outlook.Attachment attachment in attachments)
{
var attachtypevar = attachment.PropertyAccessor.GetProperty(PR_ATTACH_METHOD);
int attachtype = Convert.ToInt32(attachtypevar);
byte[] attachmentData = null;
if (attachtype == 1)
{
attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN) as byte[];
MemoryStream theMemStream = new MemoryStream();
theMemStream.Write(attachmentData, 0, attachmentData.Length);
theMemStream.Position = 0;
try
{
bool overwrite = false;
ClientContext clientContext = new ClientContext(clientContextURL);
using (theMemStream)
{
ClientOM.File.SaveBinaryDirect(clientContext, sitelibfolder + attachment.FileName, theMemStream, overwrite);
}
}
catch (Exception ex)
{
MessageBox.Show("Sharepoint URL issues. " + ex.Message);
}
}
else if (attachtype == 5)
{
object mi = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_OBJ);
MessageBox.Show(attachment.FileName + " - is not a supported attachment format for auto-upload.");
}
}
}
}
在这段代码中,首先检查附件的类型。如果是标准对象(attachtype == 1),就将其转换为字节数组,并使用MemoryStream将其上传到SharePoint。如果是嵌入的邮件(attachtype == 5),则弹出一个消息框提示用户该附件格式不支持自动上传。
用户在Outlook的邮件视图中打开邮件,然后右键点击邮件,可以看到添加的自定义按钮。点击"Attachment to SharePoint"按钮后,该邮件的所有附件将上传到代码中指定的SharePoint实例。
这个实现不支持嵌入的邮件(.msg)格式。如果邮件中有四种附件,其中三种是zip、xls、doc格式,第四种是.msg格式,那么.msg格式的附件将不会被上传到SharePoint。
这个示例只是正在进行的一个更大项目的一部分。还有一个会话扫描器、自定义分类/属性邮件标记功能(支持对类别及其属性的全文本搜索!),以及基于邮件收件人/抄送人快速设置约会的功能等。
为了提高用户体验,可以实现一个存储功能,以便用户不需要每次都输入SharePoint站点的URL。以下是实现存储功能的代码示例:
private void AddtoStorage(string storageIdentifier, string storageContent)
{
if (!String.IsNullOrEmpty(storageIdentifier) && !String.IsNullOrEmpty(storageContent))
{
Outlook.MAPIFolder folder = Globals.ThisAddIn.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.StorageItem storageitem = folder.GetStorage(storageIdentifier, Outlook.OlStorageIdentifierType.olIdentifyBySubject);
storageitem.Body = storageContent;
storageitem.Save();
}
}
private string GetfromStorage(string storageIdentifier)
{
if (!String.IsNullOrEmpty(storageIdentifier))
{
Outlook.MAPIFolder folder = Globals.ThisAddIn.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.StorageItem storageitem = folder.GetStorage(storageIdentifier, Outlook.OlStorageIdentifierType.olIdentifyBySubject);
try
{
string bodycontent = storageitem.Body.ToString();
return bodycontent;
}
catch (Exception e)
{
return "";
}
}
else
{
return "";
}
}