Feel free to add your own samples.

Note that any solution that builds these samples will need to include the appropriate assembly references, e.g. System, WindowsBase, etc.

The Simplest Address Book

This is the pretty much the Contacts equivalent of "Hello, World"

using System;
using Microsoft.Communications.Contacts;

class SimplestAddressBookEver
{
    [STAThread]
    static void Main()
    {
        foreach (Contact contact in new ContactManager().GetContactCollection())
        {
            Console.WriteLine(
                string.Format(
                    "{0}\n\tE-mail:\t\t{1}\n\tBusiness Phone:\t{2}",
                    // You can use "Default" to get the best match
                    contact.Names.Default.FormattedName,
                    contact.EmailAddresses.Default.Address,
                    // Or you can specify the labels to be more specific
                    contact.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice].Number));
        }
    }
}


Worth pointing out is the use of the Voice label. Generally the pattern in Windows Contacts is that if there are multiple labels that are semantically mutually exclusive, (e.g. Fax, Voice, Video, Pager), then the label should be explicitly provided even if there is an implied default (Voice). This makes it so requesting the "Business" phone won't accidentally return the Business Fax number.

Adding a picture to a contact

Contacts use labels to provide additional context to programs. For example, Windows Contacts uses the "UserTile" label to specify the picture that should be used for displaying in Explorer views.

using System;
using Microsoft.Communications.Contacts;

class ContactWithPictures
{
    [STAThread]
    static void Main()
    {
        ContactManager cm = new ContactManager();
        Contact contact = cm.CreateContact();

        // This is one way to add a photo.
        // You can add either a URL or an image stream.
        // Note that although remote URLs can be stored in a contact,
        // some programs, including Windows Contacts, will ignore URLs
        // that are not local to the machine.
        //
        // The type parameter for photo is the mime-type of the image.  In this case, JPEG.
        contact.Photos[PhotoLabels.UserTile] = new Photo(new Uri("D:\\MyPicture.jpg), "image/jpeg");

        // You can also add images as streams.  This approach is
        // more portable, since the image is embedded in the contact.
        using (Bitmap bitmap = new Bitmap("D:\\MyPicture.jpg"))
        {
            using (MemoryStream memstream = new MemoryStream())
            {
                bitmap.Save(memstream, ImageFormat.Jpeg);
                // By adding the preferred label here, programs will 
                // generally use this photo when looking for a UserTile
                // instead of one without "Preferred".
                contact.Photos.Add(new Photo(memstream, "image/jpeg"), PropertyLabels.Preferred, PhotoLabels.UserTile);
            }
        }
    }
}



Last edited Feb 12, 2007 at 4:08 AM by JoeCastro, version 6

Comments

tasker Dec 14, 2011 at 5:43 PM 
Please can someone help me with example code on how to create a group and add contacts to the group. How to delete the contacts in the group and then the group itself
Thanks in advance