7

I have the following code inside my sharepoint event reciever to copy a .oft file inside a document library and paste it inside a new destination (another document library):-

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);

now my code is working well. but i am not sure if i can access the .OFT file after its being copied, and modify its subject (by subject i mean i email subject) ??

VDWWD
  • 35,079
  • 22
  • 62
  • 79
John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

3

You can use Interop for that.

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");

For other users who cannot find Interop in Visual Studio, add a Reference.

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

Update

Since I do not have SharePoint (or ever worked with it), I cannot test this. But maybe something like this can work? Use the Url of the SPListItem to get the correct file. You could also try the absolute url as seen in this post. Use that string to load the file for editing.

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;
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • thanks for your reply. but inside this line of code `application.CreateItemFromTemplate("oldfile.oft") as MailItem` how i can reference the file which is already stored inside a document library, i am getting this file inside my `foreach` loop.. and seocnd question how i template file already have a Body , and i do not want to modify it .. but i only want to modify the subject is this possible ? – John John May 09 '18 at 15:08
  • 1
    I've updated my answer with something you can try. This is not tested since I do not have SharePoint. And changing the body is optional, you can remove that from my first snippet part if you do not make changes to it. – VDWWD May 09 '18 at 19:56
  • now inside my visual studio 2012 i can not find a reference named "Microsoft Outlook 15.0 Object Library".. so is this something i can install inside my VS project? and i am planning to deploy my code inside IIS.. so will i need to install outlook on that server (the sharepoint server)?? – John John May 09 '18 at 20:44
  • Yes you need Office with Outlook installed on the server as well. That is why I usually do not recommend Interop. – VDWWD May 10 '18 at 09:05
  • so what is the other appraoch to do so? i mean if i need to modify the `.oft` subject ,, then what are the other lighter approaches which i can follow, that does not require having office with Outlook installed!!! – John John May 10 '18 at 10:28
  • I haven't found any other options. I also looked for a 3rd party library but could not find one. The only alternative I can think of is to do a search and replace in the byte array of the file itself: https://codereview.stackexchange.com/questions/3226/replace-sequence-of-strings-in-binary-file or https://stackoverflow.com/questions/5132890/c-sharp-replace-bytes-in-byte – VDWWD May 11 '18 at 12:01
  • @johnG - I think that if you use dynamic you could at least save the need to install it on your workstation, as you won't get any compiler errors. e.g. `var outlookType = Type.GetTypeFromProgID("Outlook.Application",true); dynamic outlook = Activator.CreateInstance(outlookType)` then use the rest of the API mentioned in VDWWD answer (you won't have intellisense though). – Maverick Meerkat May 15 '18 at 09:52
0

If you want to avoid the need to install/use outlook, you can check out Aspose.Email.

Aspose.Email for .NET is a complete set of Email Processing APIs to be used with .NET Framework, .NET Core & Xamarin platforms, enabling you to build cross-platform applications having the ability to create, manipulate, convert and transmit emails without using Microsoft Outlook

It supports OFT files as well

Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66