Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i copy a .OFT file and change its Subject

Tags:

c#

asp.net

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

like image 427
john Gu Avatar asked May 04 '18 16:05

john Gu


People also ask

How do I edit an oft File?

Open . oft file, make required changes and click File - Save as, change file format to . oft and save in require location. Home ->New Items-> More Items->Choose Form , Select User template in File System , click on the Browse button and the "Browse For Folder" dialog will allow to browse the File System.

What is a .OFT File?

OFT (Outlook File Template) is a template format that allows sending messages with static or regular information from message to message. Additionally, it is an excellent alternative for closed corporate email systems that frown upon the use of hosted image content.

How do you save an oft File as a PDF?

Open Word and choose File > Open. Select the HTML file you saved in step 4. Choose File > Save As, and then choose PDF (*. pdf) from the file type drop-down before choosing Save.

Where are Outlook OFT files stored?

By default templates are saved in the following location: c:\users\username\appdata\roaming\microsoft\templates and you can use it to send an email message.


1 Answers

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;
    }
}
like image 131
VDWWD Avatar answered Sep 29 '22 01:09

VDWWD