powershelloutlookcontactgroups

Outlook Distribution Lists (DistListItem) are not updated after a referenced contact (ContactItem) is changed


I've gathered some code from around the web to create Contacts and then Contact groups. However, if I update the contact after creation, the "relation" between the contact object inside the Contact group and the Contact is gone. The Contact group is not updated with the changes to the Contact.

If I manually create a Contact and Contact group, the relationship is just maintained as expected. Any ideas on what I could have missed?

Code for the Contact:

$olContactItem = 2
$o = new-object -comobject outlook.application
$c = $o.CreateItem($olContactItem)
$c.FullName =  "Dummy Account"
$c.Email1Address =  "aa@bb.com"
$a = $c.Save() 

Code for the Contact group:

$outlook = new-object -com Outlook.Application
$contacts = $outlook.Session.GetDefaultFolder(10)
$session = $outlook.Session
$session.Logon("Outlook")
$namespace = $outlook.GetNamespace("MAPI")
$DL = $contacts.Items.Add("IPM.DistList")
$DL.DLName = "dummy2"
$recipient = $namespace.CreateRecipient("Dummy Account")    
$recipient.Resolve()
$DL.AddMember($recipient)
$DL.Save()

Looks pretty straight forward to me. I checked the API, but that didn't get me much further. https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/recipients-object-outlook

Thanks in advance!


Solution

  • You add $recipient before it is initialized.

    UPDATE: DistListItem.AddMember in OOM only adds one-off recipients, there is no way to add contacts. If using Redemption (I am its author) is an option, it exposes RDODistListItem.AddContact method that allows to pass either Outlook's ContactItem object or RDOContactItem object from Redemption. RDODistListItem also exposes AddMembers / AddMember / AddMemberEx methods.