首页 > 解决方案 > 我可以复制 .OFT 文件并更改其主题吗

问题描述

我的共享点事件接收器中有以下代码,用于在文档库中复制 .oft 文件并将其粘贴到新目标(另一个文档库)中:-

SPDocumentLibrary template = (SPDocumentLibrary)properties.Web.GetList(properties.Web.ServerRelativeUrl + "/Templates/");
SPListItem templetefile = null;

foreach (SPListItem i in template.Items)
{
    if (i.Name.ToLower().Contains("project"))
    {
         templetefile = i;    
    }
}

byte[] fileBytes = templetefile.File.OpenBinary();
string destUrl = properties.Web.ServerRelativeUrl + "/" + projectid.RootFolder.Url +".oft";
SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, false);

现在我的代码运行良好。但我不确定我是否可以在复制 .OFT 文件后访问它,并修改其主题(主题是指我的电子邮件主题)??

标签: c#asp.net

解决方案


您可以为此使用互操作。

using Microsoft.Office.Interop.Outlook;

Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();    
MailItem mail = application.CreateItemFromTemplate("oldfile.oft") as MailItem;

mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.Subject = "New Subject";
mail.HTMLBody = "<p>This is a new <strong>OFT</strong> file with a changed subject line.</p>";
mail.SaveAs("newfile.oft");

对于在 Visual Studio 中找不到 Interop 的其他用户,请添加参考。

References> Add Reference> COM>Microsoft Outlook 15.0 Object Library

更新

由于我没有 SharePoint(或曾经使用过它),因此我无法对此进行测试。但也许这样的事情可以工作?使用获取正确的文件UrlSPListItem您也可以尝试在这篇文章中看到的绝对 url 。使用该字符串加载文件进行编辑。

foreach (SPListItem i in template.Items)
{
    if (i.Name.ToLower().Contains("project"))
    {
        string url = i.Url;
        string absoluteUrl = (string)i[SPBuiltInFieldId.EncodedAbsUrl];

        MailItem mail = application.CreateItemFromTemplate(url) as MailItem;

        //or

        MailItem mail = application.CreateItemFromTemplate(absoluteUrl) as MailItem;
    }
}

推荐阅读