views:

530

answers:

2

I need to read records containing name and email from a file or database and add them to an existing Oulook distribution list (from the private contacts, not from the GAL).

I just saw examples of reading from OL using LINQ to DASL which I have working for mail and appointments, but I can't figure out how to list the contents of a dist list:

private static void GetContacts()
    {
         Outlook.Application app = new Outlook.Application();
         Outlook.Folder folder = (Outlook.Folder)app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
 var distLists = from item in folder.Items.AsQueryable<MyDistList>()
                 where item.DLName == "My Dist List"
                 select item.Item;

        var builder = new StringBuilder();

        foreach (var list in distLists)
        {
            builder.AppendLine(list.DLName);
            foreach (var item in list.Members)
            {
            // can't figure out how to iterate through the members here
            // compiler says Object doesn't have GeNumerator...
            }
        }

        Console.WriteLine(builder.ToString());
        Console.ReadLine();
    }

Once I can read the members I need to be able to add new ones which is even more trick. Any help would be appreciated.

A: 

OK, if no-one has experience with LINQ to DASL, can anyone share code that creates entries in a private distlist. I guess there are several ways:

  1. automate Outlook
  2. CDO
  3. Webdav
  4. other?

what's the easiest?

Graeme
Turns out it is easy enough. I was simply missing the call to Resolve as I thought that was only if you were resolving against the GAL: Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<[email protected]>"); rcp.Resolve(); list.AddMember(rcp); list.Save();
Graeme
+1  A: 

Turns out it is easy enough. I was simply missing the call to Resolve as I thought that was only if you were resolving against the GAL:

Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<[email protected]>");
rcp.Resolve();
list.AddMember(rcp);
list.Save();

And I can create an iterator that uses the distList.GetMember method:

// Wrap DistListItem.GetMembers() as an iterator

public static class DistListItemExtensions
{
    public static IEnumerable<Outlook.Recipient> Recipients(this Outlook.DistListItem distributionList)
    {
        for (int i = 1; i <= distributionList.MemberCount; i++)
        {
            yield return distributionList.GetMember(i);
        }
    }
}
Graeme