Create a custom Contact item

This example shows how to create a custom contact item and set both predefined and user-defined properties.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

A ContactItem object represents a contact in the Contacts folder, and has more than 100 built-in properties such as FirstName and LastName. Sometimes, the built-in properties are not sufficient and you need to add custom properties, which you can do by using the UserProperties collection.

In the following code example, CreateCustomItem creates a custom ContactItem object, names it "Shoe Store", and calls the Add(String, Object) method to add it to a folder named "Shoe Store". CreateCustomItem first gets the "Shoe Store" folder by using the GetDefaultFolder(OlDefaultFolders) method. The "Shoe Store" folder is a subfolder of the default Contacts folder. CreateCustomItem then sets the FirstName and LastName properties, and creates a user-defined property ("Shoe Size") by using the UserProperties collection.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void CreateCustomItem()
{
    Outlook.Folder folder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderContacts).Folders[
        "Shoe Store"] as Outlook.Folder;
    Outlook.ContactItem contact =
        folder.Items.Add(
        "IPM.Contact.Shoe Store") as Outlook.ContactItem;
    contact.FirstName = "Michael";
    contact.LastName = "Affronti";
    contact.UserProperties["Shoe Size"].Value = "9";
    contact.Save();
}

See also