Code to create or modify a contact
The following Visual Basic Scripting Edition (VBScript) code sample creates a contact that is named John Doe. If this contact already exists, it will be modified. For demonstration purposes, this code sample sets most of the attributes that are available to contacts. Only the DAV:contentclass property and http://schemas.microsoft.com/exchange/outlookmessageclass are required.Note To run this code sample, you must replace the following strings with the appropriate values wherever they exist in the code sample:
- ServerName
- UserName
- password
' Declare the variables that you will use.
Dim objRequest 'As MSXML.XMLHTTPRequest
Dim strBody 'As String
Dim strXMLNSInfo 'As String
Dim strNameInfo 'As String
Dim strBusinessAddrInfo 'As String
Dim strHomeAddrInfo 'As String
Dim strOtherAddrInfo 'As String
Dim strMailAddrInfo 'As String
Dim strPhoneInfo 'As String
Dim strEmailInfo 'As String
Dim strOrganizationalInfo 'As String
Dim strPersonalInfo 'As String
Dim strCustomerInfo 'As String
Dim strFollowUpInfo 'As String
Dim strMiscInfo 'As String
Dim strUserFieldsInfo 'As String
Dim strURL 'As String
' Specify the URL of the new object that you will create.
strURL = "http://ServerName/Exchange/UserName/Contacts/John Doe.eml"
' Create an HTTP request object.
Set objRequest = CreateObject("Microsoft.xmlhttp")
' Specify the Namespaces that you will use.
strXMLNSInfo = "xmlns:g=""DAV:"" " & _
"xmlns:c=""urn:schemas:contacts:"" " & _
"xmlns:e=""http://schemas.microsoft.com/exchange/"" " & _
"xmlns:mapi=""http://schemas.microsoft.com/mapi/"" " & _
"xmlns:x=""xml:"" xmlns:cal=""urn:schemas:calendar:"" " & _
"xmlns:mail=""urn:schemas:httpmail:"">"
' Specify the contact's name information.
' (First Name, Middle Name, Last Name, Full Name,
' Subject of the contact--used by Outlook Address Book,
' File As, Initials, Nickname, Title, and Suffix)
strNameInfo = "<c:givenName>John</c:givenName>" & _
"<c:middlename>Jacob</c:middlename>" & _
"<c:sn>Doe</c:sn>" & _
"<c:cn>John J. Doe</c:cn>" & _
"<mail:subject>John Doe</mail:subject>" & _
"<c:fileas>Doe, John</c:fileas>" & _
"<c:initials>JJD</c:initials>" & _
"<c:nickname>Johnnie</c:nickname>" & _
"<c:personaltitle>Mr.</c:personaltitle>" & _
"<c:namesuffix>MCSD</c:namesuffix>"
' Specify the Business Address Information.
' (Street, PO Box, City, State, Postal Code, and Country)
strBusinessAddrInfo = "<c:street>One Microsoft Way</c:street>" & _
"<c:postofficebox>PO Box 12345</c:postofficebox>" & _
"<c:l>Redmond</c:l>" & _
"<c:st>WA</c:st>" & _
"<c:postalcode>98052-6399</c:postalcode>" & _
"<c:co>USA</c:co>"
' Specify the Home Address Information.
' (Street, PO Box, City, State, Postal Code, and Country)
strHomeAddrInfo = "<c:homeStreet>500 Main Street</c:homeStreet>" & _
"<c:homepostofficebox>PO Box 54321</c:homepostofficebox>" & _
"<c:homeCity>Bellevue</c:homeCity>" & _
"<c:homeState>WA</c:homeState>" & _
"<c:homePostalCode>98004</c:homePostalCode>" & _
"<c:homeCountry>USA</c:homeCountry>"
' Specify the Other Address Information.
' (Street, PO Box, City, State, Postal Code, and Country)
strOtherAddrInfo = "<c:otherstreet>99 Myrtle Ave</c:otherstreet>" & _
"<c:otherpostofficebox>PO Box 98765</c:otherpostofficebox>" & _
"<c:othercity>Green Cove Springs</c:othercity>" & _
"<c:otherstate>FL</c:otherstate>" & _
"<c:otherpostalcode>32043</c:otherpostalcode>" & _
"<c:othercountry>USA</c:othercountry>"
' Specify which address is the mailing address.
' 0 = None, 1 = Home, 2 = Business, 3 = Other
strMailAddrInfo = "<c:mailingaddressid>2</c:mailingaddressid>"
' Specify the telephone number information.
' (Business Phone, Business Phone 2, Business Fax,
' Home Phone, Home Phone 2, Home Fax,
' Other Phone, Other Fax, Pager, Mobile Phone,
' Car Phone, ISDN, Telex, TTY/TDD, Callback)
strPhoneInfo = "<c:telephoneNumber>425-555-1110</c:telephoneNumber>" & _
"<c:telephonenumber2>425-555-1111</c:telephonenumber2>" & _
"<c:facsimiletelephonenumber>425-555-1112</c:facsimiletelephonenumber>" & _
"<c:homePhone>425-555-1113</c:homePhone>" & _
"<c:homephone2>425-555-1114</c:homephone2>" & _
"<c:homefax>425-555-1115</c:homefax>" & _
"<c:otherTelephone>904-555-1111</c:otherTelephone>" & _
"<c:otherfax>904-555-1112</c:otherfax>" & _
"<c:pager>425-555-1116</c:pager>" & _
"<c:mobile>425-555-1117</c:mobile>" & _
"<c:othermobile>904-555-1113</c:othermobile>" & _
"<c:internationalisdnnumber>425-555-1118</c:internationalisdnnumber>" & _
"<c:telexnumber>425-555-1119</c:telexnumber>" & _
"<c:ttytddphone>425-555-1120</c:ttytddphone>" & _
"<c:callbackphone>425-555-1121</c:callbackphone>"
' Specify the e-mail address information.
strEmailInfo = "<mapi:emaillisttype>1</mapi:emaillisttype>" & _
"<mapi:email1addrtype>EX</mapi:email1addrtype>" & _
"<mapi:email1emailaddress>" & _
"/o=Microsoft/ou=First Administrative Group/cn=Recipients/cn=jdoe" & _
"</mapi:email1emailaddress>" & _
"<mapi:email1originaldisplayname>" & _
"John Doe (Exchange)" & _
"</mapi:email1originaldisplayname>" & _
"<mapi:email2addrtype>SMTP</mapi:email2addrtype>" & _
"<mapi:email2emailaddress>" & _
"john.doe@whatever.domain.com" & _
"</mapi:email2emailaddress>" & _
"<mapi:email2originaldisplayname>" & _
"John Doe (SMTP)" & _
"</mapi:email2originaldisplayname>" & _
"<mapi:email3addrtype>X400</mapi:email3addrtype>" & _
"<mapi:email3emailaddress>" & _
"c=us;a= ;p=Microsoft;o=Exchange;s=Doe;g=John;i=J;" & _
"</mapi:email3emailaddress>" & _
"<mapi:email3originaldisplayname>" & _
"John Doe (X400)" & _
"</mapi:email3originaldisplayname>"
' Specify the organizational information.
' (Company, Company Main Phone, Business Home Page,
' Department, Job Title, Manager's Name,
' Assistant's Name, Assistant's Phone, Office Location,
' Organizational ID Number, Computer Network Name,
' Profession)
strOrganizationalInfo = "<c:o>Microsoft Corporation</c:o>" & _
"<c:organizationmainphone>425-882-8080</c:organizationmainphone>" & _
"<c:businesshomepage>http://www.microsoft.com</c:businesshomepage>" & _
"<c:department>YYY</c:department>" & _
"<c:title>Lead Software Design Engineer</c:title>" & _
"<c:manager>Ashley Doe</c:manager>" & _
"<c:secretarycn>Pat Doe</c:secretarycn>" & _
"<c:secretaryphone>425-555-1122</c:secretaryphone>" & _
"<c:roomnumber>C-309</c:roomnumber>" & _
"<c:employeenumber>987654321</c:employeenumber>" & _
"<c:computernetworkname>jdoe</c:computernetworkname>" & _
"<c:profession>Software Designer</c:profession>"
' Specify the personal information.
' (Birthday, Anniversary, Spouse, Children, Gender,
' Personal Home Page, Hobbies)
strPersonalInfo = "<c:bday>1974-01-01T08:00:00Z</c:bday>" & _
"<c:weddinganniversary>1995-01-01T08:00:00Z</c:weddinganniversary>" & _
"<c:spousecn>Jane Doe</c:spousecn>" & _
"<c:childrensnames>" & _
"<x:v>Ellen Doe</x:v><x:v>Grace Doe</x:v>" & _
"</c:childrensnames>" & _
"<c:gender>Male</c:gender>" & _
"<c:personalHomePage>" & _
"http://www.homepagedomain.com/JohnDoe/default.htm" & _
"</c:personalHomePage>" & _
"<c:hobbies>Playing XBox games,Watching UltimateTV</c:hobbies>"
' Specify the customer-related information.
' (Customer ID, Account, Billing Information)
strCustomerInfo = "<c:customerid>YYY</c:customerid>" & _
"<c:account>YYY</c:account>" & _
"<c:billinginformation>YYY</c:billinginformation>"
' Specify the Follow Up/Reminder information.
' (Reminder, Reminder Topic, Reminder Time, and so on.)
strFollowUpInfo = "<mapi:reminderset>1</mapi:reminderset>" & _
"<mapi:request>Call</mapi:request>" & _
"<mapi:remindertime>2001-12-01T08:00:00Z</mapi:remindertime>" & _
"<mapi:remindernexttime>2001-12-01T08:00:00Z</mapi:remindernexttime>" & _
"<e:reply-by-iso>2001-12-01T08:00:00Z</e:reply-by-iso>"
' Specify any miscellaneous information.
' (Categories, Contacts, Mileage, FTP Site, Language,
' Government ID, Location, Internet Free/Busy Address,
' Sensitivity)
strMiscInfo = "<e:keywords-utf8>" & _
"<x:v>Buddies</x:v><x:v>Engineers</x:v>" & _
"</e:keywords-utf8>" & _
"<mapi:contacts><x:v>Penelope Doe</x:v></mapi:contacts>" & _
"<e:mileage>Rarely used string property</e:mileage>" & _
"<c:ftpsite>ftp://ftp.microsoft.com/</c:ftpsite>" & _
"<c:language>US English</c:language>" & _
"<c:governmentid>000-00-0000</c:governmentid>" & _
"<c:location>Nowhere Land</c:location>" & _
"<cal:fburl>http://www.homepagedomain.com/JohnDoe/freebusy</cal:fburl>" & _
"<mapi:sensitivity>2</mapi:sensitivity>"
' Specify the User Field information.
' (User Field 1, User Field 2, User Field 3, User Field 4)
strUserFieldsInfo = "<e:extensionattribute1>User Data 1" & _
"</e:extensionattribute1>" & _
"<e:extensionattribute2>User Data 2</e:extensionattribute2>" & _
"<e:extensionattribute3>User Data 3</e:extensionattribute3>" & _
"<e:extensionattribute4>User Data 4</e:extensionattribute4>"
' Put all the information together in an HTTP request.
strBody = "<?xml version=""1.0""?>" & _
"<g:propertyupdate " & strXMLNSInfo & _
"<g:set>" & _
"<g:prop>" & _
"<g:contentclass>urn:content-classes:person</g:contentclass>" & _
"<e:outlookmessageclass>IPM.Contact</e:outlookmessageclass>" & _
strNameInfo & strBusinessAddrInfo & _
strHomeAddrInfo & strOtherAddrInfo & _
strMailAddrInfo & strPhoneInfo & _
strEmailInfo & strOrganizationalInfo & _
strPersonalInfo & strCustomerInfo & _
strFollowUpInfo & strMiscInfo & _
strUserFieldsInfo & _
"</g:prop>" & _
"</g:set>" & _
"</g:propertyupdate>"
' Open the request object and assign the PROPPATCH method to it.
objRequest.open "PROPPATCH", strURL, False, "UserName", "password"
' Set the required headers for the request.
objRequest.setRequestHeader "Content-Type", "text/xml"
objRequest.setRequestHeader "Translate", "f"
objRequest.setRequestHeader "Content-Length", Len(strBody)
' Send the request. Use the XML document as the body.
objRequest.send strBody
'Display the results.
If (objRequest.Status >= 200 And objRequest.Status < 300) Then
MsgBox "Success! " & "Results = " & objRequest.Status & _
": " & objRequest.statusText
ElseIf objRequest.Status = 401 Then
MsgBox "You do not have permission to do the job. " & _
"Please check your permissions on this item."
Else
MsgBox "Request Failed. Results = " & objRequest.Status & _
": " & objRequest.statusText
End If
Set objRequest = Nothing
Code to delete a contact
The following VBScript code sample deletes the contact that you created by using the previous code sample.Note To run this code sample, you must replace the following strings with the appropriate values wherever they exist in the code sample:
- ServerName
- UserName
- password
' Declare the variables that you will use.
Dim objRequest 'As MSXML.XMLHTTPRequest
Dim strURL 'As String
' Specify the URL of the object that you will delete.
strURL = "http://ServerName/Exchange/UserName/Contacts/John Doe.eml"
' Create an HTTP request object.
Set objRequest = CreateObject("Microsoft.xmlhttp")
' Open the object and assign a method to the object.
objRequest.open "DELETE", strURL, False, "UserName", "password"
' Send the request. Use the XML document as the body.
objRequest.send
'Display the results.
If (objRequest.Status >= 200 And objRequest.Status < 300) Then
MsgBox "Success! " & "Results = " & objRequest.Status & _
": " & objRequest.statusText
ElseIf objRequest.Status = 401 Then
MsgBox "You do not have permission to do the job. " & _
"Please check your permissions on this item."
Else
MsgBox "Request Failed. Results = " & objRequest.Status & _
": " & objRequest.statusText
End If
Set objRequest = Nothing