Tuesday, May 15, 2018

Can i copy a .OFT file and change its Subject

Leave a Comment

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

1 Answers

Answers 1

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;     } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment