Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to create a Contact item by using the HttpWebRequest class and the HttpWebResponse class in Visual Basic .NET


View products that this article applies to.

This article was previously published under Q314190

↑ Back to the top


Summary

This article describes how to use the HttpWebRequest class and the HttpWebResponse class of the "System.Net" namespace to create a Contact item. This article describes how to create the Contact item on a computer that is running Microsoft Exchange 2000 Server by using Microsoft Visual Basic .NET.

↑ Back to the top


More information

To create a Contact item on a computer that is running Exchange 2000, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects types list, click Console Application.

    By default, the Module1.vb file is created.
  4. In the code window, replace the code with the following:
    Imports System.Net
    Imports System.IO
    
    Module Module1
        Sub Main()
            ' TODO: Replace with the contact item's URL. 
            Dim sUri As String = "http://<ExchServer>/Exchange/<UserName>/Contacts/JoLynnDobney.eml"
           
            Dim myUri As System.Uri = New System.Uri(sUri)
            Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)
    
            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
    
            ' Specify the namespaces that you want to 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>JoLynn</c:givenName>" & _
             "<c:middlename>Julie</c:middlename>" & _
             "<c:sn>Dobney</c:sn>" & _
             "<c:cn>JoLynn J. Dobney</c:cn>" & _
             "<mail:subject>JoLynn Dobney</mail:subject>" & _
             "<c:fileas>Dobney, JoLynn</c:fileas>" & _
             "<c:initials>JJD</c:initials>" & _
             "<c:nickname>Jo</c:nickname>" & _
             "<c:personaltitle>Mrs.</c:personaltitle>" & _
             "<c:namesuffix>MCSD</c:namesuffix>"
    
            ' Specify the business address information.
            ' (Street, PO Box, City, State, Postal Code, and Country)
            strBusinessAddrInfo = "<c:street>Business Address Information</c:street>" & _
             "<c:postofficebox>PO Box 12345</c:postofficebox>" & _
             "<c:l>AnyTown</c:l>" & _
             "<c:st>WA</c:st>" & _
             "<c:postalcode>12345-1234</c:postalcode>" & _
             "<c:co>United States</c:co>"
    
            ' Specify the home address information.
            ' (Street, PO Box, City, State, Postal Code, and Country)
            strHomeAddrInfo = "<c:homeStreet>Home Address Information</c:homeStreet>" & _
             "<c:homepostofficebox>PO Box 54321</c:homepostofficebox>" & _
             "<c:homeCity>SomeTown</c:homeCity>" & _
             "<c:homeState>WA</c:homeState>" & _
             "<c:homePostalCode>12345</c:homePostalCode>" & _
             "<c:homeCountry>United States</c:homeCountry>"
    
            ' Specify the other address information.
            ' (Street, PO Box, City, State, Postal Code, and Country)
            strOtherAddrInfo = "<c:otherstreet>Other Address Information</c:otherstreet>" & _
             "<c:otherpostofficebox>PO Box 98765</c:otherpostofficebox>" & _
             "<c:othercity>SomeTown</c:othercity>" & _
             "<c:otherstate>FL</c:otherstate>" & _
             "<c:otherpostalcode>56789</c:otherpostalcode>" & _
             "<c:othercountry>United States</c:othercountry>"
    
            ' Specify the mailing address.
            ' 0 = None, 1 = Home, 2 = Business, 3 = Other
            strMailAddrInfo = "<c:mailingaddressid>2</c:mailingaddressid>"
    
            ' Specify the phone 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-0110</c:telephoneNumber>" & _
             "<c:telephonenumber2>425-555-0111</c:telephonenumber2>" & _
             "<c:facsimiletelephonenumber>425-555-0112</c:facsimiletelephonenumber>" & _
             "<c:homePhone>425-555-0113</c:homePhone>" & _
             "<c:homephone2>425-555-0114</c:homephone2>" & _
             "<c:homefax>425-555-0115</c:homefax>" & _
             "<c:otherTelephone>206-555-0111</c:otherTelephone>" & _
             "<c:otherfax>206-555-0112</c:otherfax>" & _
             "<c:pager>425-555-0116</c:pager>" & _
             "<c:mobile>425-555-0117</c:mobile>" & _
             "<c:othermobile>206-555-0113</c:othermobile>" & _
             "<c:internationalisdnnumber>425-555-0118</c:internationalisdnnumber>" & _
             "<c:telexnumber>425-555-0119</c:telexnumber>" & _
             "<c:ttytddphone>425-555-0120</c:ttytddphone>" & _
             "<c:callbackphone>425-555-0121</c:callbackphone>"
    
            ' Specify the e-mail address information.
            strEmailInfo = "<mapi:email1addrtype>EX</mapi:email1addrtype>" & _
             "<mapi:email1emailaddress>" & _
             "/o=Microsoft/ou=First Administrative Group/cn=Recipients/cn=jdobney" & _
             "</mapi:email1emailaddress>" & _
             "<mapi:email1originaldisplayname>" & _
             "JoLynn Dobney (Exchange)" & _
             "</mapi:email1originaldisplayname>" & _
             "<mapi:email2addrtype>SMTP</mapi:email2addrtype>" & _
             "<mapi:email2emailaddress>" & _
             "JoLynn.Dobney@example.com" & _
             "</mapi:email2emailaddress>" & _
             "<mapi:email2originaldisplayname>" & _
             "JoLynn Dobney (SMTP)" & _
             "</mapi:email2originaldisplayname>" & _
             "<mapi:email3addrtype>X400</mapi:email3addrtype>" & _
             "<mapi:email3emailaddress>" & _
             "c=us;a= ; p=Microsoft; o=Exchange; s=Dobney; g=JoLynn; i=J;" & _
             "</mapi:email3emailaddress>" & _
             "<mapi:email3originaldisplayname>" & _
             "JoLynn Dobney (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>Fourth Coffee </c:o>" & _
             "<c:organizationmainphone>425-555-0199</c:organizationmainphone>" & _
             "<c:businesshomepage>http://www.fourthcoffee.com</c:businesshomepage>" & _
             "<c:department>YYY</c:department>" & _
             "<c:title>Lead Software Design Engineer</c:title>" & _
             "<c:manager>Karan Khanna</c:manager>" & _
             "<c:secretarycn>Ken Myer</c:secretarycn>" & _
             "<c:secretaryphone>425-555-0123</c:secretaryphone>" & _
             "<c:roomnumber>C-309</c:roomnumber>" & _
             "<c:employeenumber>987654321</c:employeenumber>" & _
             "<c:computernetworkname>jdobney</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>Barry Johnson</c:spousecn>" & _
             "<c:childrensnames>" & _
             "<x:v>David Johnson</x:v><x:v>Willis Johnson</x:v>" & _
             "</c:childrensnames>" & _
             "<c:gender>Male</c:gender>" & _
             "<c:personalHomePage>" & _
             "http://www.example.com/JoLynnDobney/default.htm" & _
             "</c:personalHomePage>" & _
             "<c:hobbies>Reading, lawn bowling</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 follow-up information and reminder information.
            ' (Reminder, Reminder Topic, Reminder Time)
            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 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>Brian Johnson</x:v></mapi:contacts>" & _
             "<e:mileage>Rarely used string property</e:mileage>" & _
             "<c:ftpsite>ftp://ftp.example.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.example.com/JoLynnDobney/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>"
    
            Dim sQuery As String
            sQuery = "<?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>"
    
            ' Set the credentials.
            ' TODO: Replace with the appropriate user credential
            Dim myCred As NetworkCredential = New NetworkCredential("Domain\UserName", "Password")
            Dim MyCredentialCache As CredentialCache = New CredentialCache()
            MyCredentialCache.Add(myUri, "Basic", myCred)
            HttpWRequest.Credentials = MyCredentialCache
    
            ' Set the headers.
            HttpWRequest.KeepAlive = False
            HttpWRequest.Headers.Set("Pragma", "no-cache")
            HttpWRequest.Headers.Set("Translate", "f")
            HttpWRequest.ContentType = "text/xml"
            HttpWRequest.ContentLength = sQuery.Length
    
            'Set the request timeout to 5 minutes.
            HttpWRequest.Timeout = 300000
            ' set the request method
            HttpWRequest.Method = "PROPPATCH"
    
            ' Store the data in a byte array.
            Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery)
            HttpWRequest.ContentLength = ByteQuery.Length
            Dim QueryStream As Stream = HttpWRequest.GetRequestStream()
            ' write the data to be posted to the Request Stream
            QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
            QueryStream.Close()
    
            ' Send the request and get the response.
            Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse()
    
            ' Get the status and the headers.
            Dim iStatCode As Integer = HttpWResponse.StatusCode
            Dim sStatus As String = iStatCode.ToString()
            Console.WriteLine("Status: {0} {1}", sStatus, HttpWResponse.StatusDescription.ToString())
            Console.WriteLine("Request Headers:")
            Console.WriteLine(HttpWRequest.Headers.ToString())
            Console.WriteLine("Response Headers:")
            Console.WriteLine(HttpWResponse.Headers.ToString())
    
            ' Get the response stream.
            Dim strm As Stream = HttpWResponse.GetResponseStream()
    
            ' Read the response stream.
            Dim sr As StreamReader = New StreamReader(strm)
            Dim sText As String = sr.ReadToEnd()
            Console.WriteLine("Response: {0}", sText)
    
            ' Close the stream.
            strm.Close()
    
            ' Clean up
            HttpWRequest = Nothing
            HttpWResponse = Nothing
            MyCredentialCache = Nothing
            myCred = Nothing
            strm = Nothing
            QueryStream = Nothing
            sr = Nothing
        End Sub
    End Module
  5. Search for the TODO text string in the code, and then modify the code for your environment.
  6. Press the F5 key to build and to run the program.
  7. Make sure that the Contact item is created.

↑ Back to the top


Keywords: KB314190, kbhowto, kbxml

↑ Back to the top

Article Info
Article ID : 314190
Revision : 3
Created on : 4/14/2004
Published on : 4/14/2004
Exists online : False
Views : 473