Outlook Object Model: How to assign a Webpage to a specific Outlook folder programmatically?

In this post we will see how to assign a web page to a specific Outlook folder programmatically using C# and Outlook Object Model. For this I tried the following sample, which checks for a folder named “HtmlView” in Microsoft Office Outlook. If the folder does not exist, the code creates the folder and assigns a Web page to it. If the folder exists, the code displays the folder contents.

The following sample is written using C#.Net, Visual Studio 2010 and Outlook 2010 (Outlook Object Model API).

Code snippet:

    1: //Microsoft.Office.Interop.Outlook._Application
    2: // Create an Outlook application.
    3: Outlook._Application oApp = new Outlook.Application();
    4:  
    5: // Get the MAPI NameSpace and Logon values.
    6: Outlook._NameSpace oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");
    7: oNS.Logon(Missing.Value, Missing.Value, true, true);
    8:  
    9: //Assign a Web page with a specific Outlook folder
   10: Outlook.MAPIFolder newView = null;
   11: string viewName = "HtmlView";
   12: Outlook.MAPIFolder inBox = (Outlook.MAPIFolder) oApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
   13: Outlook.Folders searchFolders = (Outlook.Folders)inBox.Folders;
   14: bool foundView = false;
   15:  
   16: foreach (Outlook.MAPIFolder searchFolder in searchFolders)
   17: {
   18:    if (searchFolder.Name == viewName)
   19:       {
   20:          newView = inBox.Folders[viewName];
   21:          foundView = true;
   22:       }
   23: }
   24:  
   25: if (!foundView)
   26: {
   27:      newView = (Outlook.MAPIFolder)inBox.Folders. Add("HtmlView", Outlook.OlDefaultFolders.olFolderInbox);
   28:      newView.WebViewURL = "http://www.microsoft.com";
   29:      newView.WebViewOn = true;
   30: }
   31:  
   32: oApp.ActiveExplorer().SelectFolder(newView);
   33: oApp.ActiveExplorer().CurrentFolder.Display();

Run the sample and you will see the following output in Outlook:

Outlook 2010 - Output

Happy programming!!