Thursday, September 23, 2004

Copying Contacts from One Mailbox to Another via Script

I had this request from one of my customers recently who wanted me to copy all the contacts from one mailbox into another. The easiest way to do this would have been to use outlook to export the contacts folder to a pst and then just import them back into the other mailbox using Outlook. Because i manage this client remotely and there's no Outlook on the Exchange server itself (and i really didn't feel like talking the users though this) I came up with the following Exoledb/CDOEX script to do the copy. The challenge when copying anything between mailbox's or public folders is you cant do a direct copy (eg ADO copyrecord only works within a mailbox or public store) so you have to use another solution.

What this script does is first does a query to get all the URL's of contacts in the mailbox's contact folder. Then it opens up each contact using the CDO.Person interface and then access's the vcard stream on each contact object . The script then copies this vcard stream into a new CDO.Person object and then saves it to the store which will then gernerate all the nessasary MAPI contact properties based on the information that is in the vCard Stream. The vCard stream has most of the information needed to create the contact as it was in the source mailbox but there are a few specific MAPI properties that are missing to be able to replicate this exactly. The first of these is the fileas property which ensures that when you recreate the contact they will be listed in the same order as they where in the source mailbox. The other import one that needs to be copied is the postal address, the business address gets copied in the vCard stream but the postal address doesn't and Outlook actually uses the postal address to display the address in the normal contact view . Some other things that don't copy over properly when you use the vCard stream is the email displayname and if you have a X400 address instead of a SMTP address this also get mangled so I've included code that copies each of those address properties manually. I've only included the properties for the first email contact field (if you want the 2nd and 3rd you'll have to get them yourself).

I've posted a copy of the script up here http://msgdev.mvps.org/exdevblog/copcont2.zip

here's what the code looks like

on error resume next
Set Rs = CreateObject("ADODB.Recordset")
set Rec = CreateObject("ADODB.Record")
set contobj = createobject("CDO.Person")
Set Conn = CreateObject("ADODB.Connection")
contactfolderurl = "file://./backofficestorage/domain.com/MBX/sourcemb/contacts/"
Conn.Provider = "ExOLEDB.DataSource"
Rec.Open contactfolderurl, ,3
SSql = "Select ""DAV:href"", ""http://schemas.microsoft.com/exchange/permanenturl"" "
SSql = SSql & " FROM scope('shallow traversal of """ & contactfolderurl & """') "
SSql = SSql & "WHERE ""DAV:contentclass"" = 'urn:content-classes:person' and ""DAV:ishidden"" = false"
Rs.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
Rs.CursorType = 3
rs.open SSql, rec.ActiveConnection, 3
if Rs.recordcount <> 0 then
Rs.movefirst
while not rs.eof
set contobj1 = createobject("CDO.Person")
wscript.echo rs.fields("DAV:href")
ourl = rs.fields("http://schemas.microsoft.com/exchange/permanenturl")
contobj.datasource.open ourl,,3
set stm = contobj.getvcardstream()
set stm1 = contobj1.getvcardstream()
stm1.writetext = stm.readtext
stm1.flush
contobj1.fields("urn:schemas:contacts:fileas") = contobj.fields("urn:schemas:contacts:fileas")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x8080") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x8080")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818A") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818A")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818B") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818B")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818C") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818C")
contobj1.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818D") = contobj.fields("http://schemas.microsoft.com/mapi//id/{00062004-0000-0000-C000-000000000046}/0x818D")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A15001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A15001E")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2A001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2A001E")
contobj1.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2B001E") = contobj.fields("http://schemas.microsoft.com/mapi/proptag/0x3A2B001E")
contobj1.fields.update
contobj1.datasource.savetocontainer "file://./backofficestorage/domain.com/MBX/targetmb/contacts/"
wscript.echo err.description
err.clear
set stm = nothing
set stm1 = nothing
set contobj1 = nothing
rs.movenext
wend
end if

4 comments:

Anonymous said...

Glen,
I am appreciative of your blog and dev resources made available although I am simply a messaging engineer and not a developer.
If you have a brief sec, could I ask a question regarding your vbscript which manages contacts between mailboxes?

Where in the script re: copy contacts is the source and target definitions for the mailbox objects?
What is used in place of the mapi id statements -- a local MAPI profile used by my user id?

My big challenge currently is getting a pfolder contact folder to sync with personal contacts apart from a blackberry device PIM sync.

Can you direct my efforts or shed light on your code re: source and target mbox objects so that I could copy from a pfolder and target a users contact folder in a script?
Thanks for your time.
Jeff
(Jgurley@ uspsoig dot gov)

Glen said...

The source and destination varibles of this script are based around Exoldb File URL's. If you want to change the source of this script to a public folder you just need to use the public folder file URI which would look something like
contactfolderurl = "file://./backofficestorage/domain.com/public folders/pub-contactsfolder"

The domain you use should be what your default SMTP domain is for your Exchange org. If you want more details on the file Uri you can have a read of http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_exch2k_the_file_url_scheme.asp . This should also work with the HTTP URI scheme so you should be able to use the URL from OWA.

Another option to this is to use CDO 1.2 to do the copy I've posted up a sample of doing this using CDO 1.2 and talked about the advantages here http://gsexdev.blogspot.com/2005/05/script-to-copy-public-folder-contacts.html

Note both of these scripts do a dumb copy not a replication so if the contact already exists in the mailbox these scripts will just duplicate the entry. Replication takes a lot more logic and lines of code. eg Active Sync

Essam said...

Is this technique available for calendar also ? Can we get an iCal stream from CDO.Appointment and create another one from that iCal stream ?
I tried the CopyRecord for an appointment to save it to a public folder and it fails , so I hope your solution with contacts can be used in calendar

Glen said...

You can use the ical stream to create an appointment in another mailbox but you will potentially loose some mapi properties. You can use CDO 1.2 to copy between stores and not loose any mapi properties. Copyrecord/moverecord can only be used within a store not between two different mailboxes or mailbox to public folder