Programming samples that can reference items and folders in Outlook by using Visual C# .NET

Article translations Article translations
Article ID: 310244 - View products that this article applies to.
This article was previously published under Q310244
Expand all | Collapse all

On This Page

INTRODUCTION

The Microsoft Outlook object model is generally used to access various types of items in folders. This article contains samples of the methods, the properties, and the objects that you can use to refer to Outlook items and Outlook folders by using Microsoft Visual C# .NET.

This article contains the following topics and their associated samples:
  • Referencing existing folders
    • GetDefaultFolder method
    • Folders object
    • Parent property
    • GetSharedDefaultFolder method
    • GetFolderFromID method
  • Creating new folders and referencing new folders
    • Folders.Add method
  • Creating new items and referencing new items
    • CreateItem method
    • Items.Add method
    • CreateItemFromTemplate method
  • Referencing existing items
    • Using Items(I) method
    • Using Items("Subject") method
    • Find method
    • Restrict method
    • GetItemFromID method

MORE INFORMATION

To add the sample code to start the application, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Visual C# .NET Projects types, click Console Application.

    By default, Class1.cs is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft Outlook 10.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK to accept your selections.
    4. Click Yes if you receive a message to generate wrappers for the libraries that you selected.
  5. In the code window, locate the following line of code.
    // TODO: Add code here to start the application.
    Read through the samples that follow. Replace the previous line of code with the sample code that you want to try.

Referencing existing folders

GetDefaultFolder method

The default folders are those folders that are at the same level as your Inbox that receives your incoming mail. If you have more than one Inbox in your profile, you can press CTRL+SHIFT+I to select your default Inbox. The default folders are those folders that you work with regularly, such as the Calendar folder, the Contacts folder, and the Tasks folder. You can easily refer to these folders by using the GetDefaultFolder method. The GetDefaultFolders method takes one argument that defines the type of folder that you want to refer to. An Outlook object has an enumeration that you can select. This enumeration is defined in the Outlook.OlDefaultFolders enum type. The following sample code assigns the oFolder object variable to the default Inbox folder.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNS = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Console.Write(oFolder.Name);

Folders object

You can use the Folders object to refer to any folder that is in the Outlook folder list. The Folders object is typically used to refer to a Microsoft Exchange folder or to any other folder that is not a default Outlook folder.

The following sample code illustrates how to refer to a public folder that is named My Public Folder. Notice that you typically start at the top-most folder and then work your way down to the folder that you have to reference. Also notice that the folder names are case-sensitive. You must exactly match the capitalization and the names as they appear in the Outlook folders list.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNS = olApp.GetNamespace("MAPI");Outlook._Folders oFolders;
oFolders = olNS.Folders;
Outlook.MAPIFolder oPublicFolder = oFolders.Item("Public Folders");
oFolders = oPublicFolder.Folders;
Outlook.MAPIFolder oAllPFolder = oFolders.Item("All Public Folders");
oFolders = oAllPFolder.Folders;
Outlook.MAPIFolder oMyFolder  = oFolders.Item("My Public Folder");
Console.Write(oMyFolder.Name);

Parent property

If you already have a reference to an Outlook item or to an Outlook folder, you can use the Parent property of the Outlook item or of the Outlook folder to create a reference to the folder that the item or the folder is located in.

The following sample code returns the name of a folder for a particular item.
Parent :
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._MailItem oMailItem = (Outlook._MailItem)  olApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.MAPIFolder oFolder = (Outlook.MAPIFolder) oMailItem.Parent;
Console.Write(oFolder.Name);

GetSharedDefaultFolder method

You can use this method if another person has granted you permission to one of their default folders. You use the GetSharedDefaultFolder method as you use the GetDefaultFolder method, except that you must specify one additional argument. The one additional argument is the name of the folder that belongs to the other person. This is the folder that you want to reference. The following sample code resolves the name of the other person to verify that the name of the other person is valid, and that the name of the other person can be used with the GetSharedDefaultFolder method.
GetSharedDefaultFolder:

// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.Recipient oRecip = olNs.CreateRecipient("Brian Baker");
oRecip.Resolve();
if (oRecip.Resolved) 
{
Outlook.MAPIFolder oFolder = olNs.GetSharedDefaultFolder(oRecip,Outlook.OlDefaultFolders.olFolderCalendar);
Console.Write(oFolder.Name);
}
For more information about how to access information in folders that belong to other people, click the following article number to view the article in the Microsoft Knowledge Base:
290824 How to open another user's calendar or another folder in Outlook 2002

GetFolderFromID method

This method is typically used with more complex solutions where a solution keeps track of both the EntryID field and the StoreID field of a folder so that the folder can be quickly referenced later. For additional information, see article 293152 in the "References" section.

Creating new folders and referencing new folders

Folders.Add method

When you use the Folders.Add method by using the Add method of the Folders collection, you can create a new folder. The first argument specifies the name of the folder, and the second argument specifies the type of the folder. The following sample code adds a Personal Tasks subfolder to your default Tasks folder.
Folders.Add Method:
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oTasks = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
Outlook.Folders oFolders = oTasks.Folders;
Outlook.MAPIFolder  oPersonalTasks = oFolders.Add("Personal Tasks",Outlook.OlDefaultFolders.olFolderTasks);
Console.Write(oPersonalTasks.Name);

Creating new items and referencing new items

CreateItem method

The CreateItem method creates a new default Outlook item. If you have to create an item that is based on a custom form that you have created, use the Items.Add method that is discussed in the next sample. You can find the CreateItem method located in the top-level application object in the Outlook object model. The CreateItem method requires only one argument. This one argument is a constant that indicates the type of item to create.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._MailItem oMailItem = (Outlook._MailItem) olApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.Display(false);

Items.Add method

When you use the Add method on the Items collection, you can create a new item that is based on any message class. The message class may be a default Outlook message class, such as the IPM.Contact message class, or a message class for a custom form, such as the IPM.Contact.MyForm message class. To use the Items.Add method, you must first reference the folder where you want to create a new item.

For more information about message classes, click the following article numbers to view the articles in the Microsoft Knowledge Base:
290657 Description of form definitions and one-off forms in Outlook 2002
290659 How to update existing items to use a new custom form
The following sample code uses the Items.Add method to create a new item that is based on a custom contact form that is named MyContact.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook._Items oItems = oContacts.Items;
Outlook._ContactItem oCustomContact = (Outlook._ContactItem) oItems.Add("IPM.Contact.MyContact");
oCustomContact.Display(false);
The following sample code uses the Items.Add method to create a new default contact item.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook._Items oItems = oContacts.Items;
Outlook._ContactItem oContact = (Outlook._ContactItem) oItems.Add("IPM.Contact");
oContact.Display(false);
Note If you use the Items.Add method, the default form for the folder is not important. You can specify any valid message class as long as the message class has been published in the folder, in the personal forms library, or in the organizational forms library.

CreateItemFromTemplate method

You can use the CreateItemFromTemplate method to create a new item that is based on an Outlook template file (.oft) format or on a message file (.msg) format. Because most forms are published in a folder or in a forms library, this method is not generally used. However, you may want to use this method to create a Setup program that is used to install forms for an Outlook solution. You typically do this for users who do not have network access or for users who work offline in Outlook. You want to use the Setup program for the following reasons:
  • The Setup program can be used to automate Outlook.
  • The Setup program uses the CreateItemFromTemplate method to open a custom form from a network share or from a disk.
  • The Setup program uses the Outlook object model to publish the form for later use.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook._ContactItem oContact = (Outlook._ContactItem)olApp.CreateItemFromTemplate("C:\\MyContact.oft",oContacts);
Outlook.FormDescription oForm = oContact.FormDescription;
oForm.Name = "My Contact";
oForm.PublishForm(Outlook.OlFormRegistry.olFolderRegistry,oContacts);
oContact.Close(Outlook.OlInspectorClose.olSave);

Referencing existing items

Using Items(I) method

You can use the Using Items(I) method to loop through all the items in a folder. The Items collection contains all the items in a particular folder. You can specify the item that you want to reference by using an index with the Items collection. This is typically used with the For loop construct.

The following sample code uses the Items(I) method approach to loop through all the contacts in the Contacts folder and then to display their FullName field in a dialog box.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
for (int i = 1; i <= oItems.Count; i++) 
{
     Outlook._ContactItem oContact = (Outlook._ContactItem) oItems.Item(i);
     Console.WriteLine(oContact.FullName);
     oContact = null;
}

Using Items("Subject") method

You can use the Using Items("Subject") method with the Items collection. You do this to specify a test string that matches the Subject field of an item.

The following sample code displays an item in the Inbox with a subject line that contains "You must help on Friday!"
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oInbox = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items oItems = oInbox.Items;
Outlook._MailItem oMail = (Outlook._MailItem) oItems.Item ("You must help on Friday!");
oMail.Display(false);

Find method

You can use the Find method to search for an item in a folder. The search is based on the value of one of the fields for the item. If the Find method is successful, you can then use the FindNext method to look for additional items that meet the same criteria.

The following sample code searches to see if you have any high priority tasks.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oTasks = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
Outlook.Items oItems = oTasks.Items;
string sFilter;
sFilter = "[Priority] = 'High'";
Outlook._TaskItem oTask = (Outlook._TaskItem) oItems.Find(sFilter);
if (oTask != null) 
{
     Console.WriteLine("You have work to do.");
     Console.WriteLine(oTask.Subject + " is still Pending");
}
else 
     Console.Write("Nothing important. Go party.");

Restrict method

The Restrict method is similar to the Find method. However, instead of returning a single item, the Restrict method returns a collection of items that meet the search criteria. For example, you can use this method to find all contacts who work for the same company.

The following sample code writes the FullName property of all the contacts who work for the A. Datum Corporation.
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
string sFilter;
sFilter = "[Company] = 'A. Datum Corporation'";
Outlook.Items oRestrictedItems = oItems.Restrict(sFilter);
for (int i=1 ;i<=oRestrictedItems.Count;i++) 
{
   Outlook._ContactItem oContactItem = (Outlook._ContactItem) oRestrictedItems.Item(i);
   Console.WriteLine(oContactItem.FullName);
   oContactItem = null; 
}

GetItemFromID method

You can use the GetItemFromID method in more complex solutions where you want the solution to keep track of both the EntryID field and the StoreID field of an item. You want to do this so that the item can be quickly referenced later. For additional information, see article 293152 in the "References" section.

REFERENCES

For more information about how to use the GetFolderFromID method, click the following article number to view the article in the Microsoft Knowledge Base:
293152 Programming with EntryIDs and StoreIDs

Properties

Article ID: 310244 - Last Review: March 15, 2006 - Revision: 5.5
APPLIES TO
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kboutlookobj kbcode kbhowto KB310244

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com