Microsoft KB Archive/314190

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

Article ID: 314190

Article Last Modified on 4/14/2004

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q314190



SUMMARY
This article describes how to use the HttpWebRequest class and the HttpWebResponse class of the &quot;System.Net&quot; 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.



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

By default, the Module1.vb file is created.  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 = &quot;http:///Exchange//Contacts/JoLynnDobney.eml&quot; 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 = &quot;xmlns:g=&quot;&quot;DAV:&quot;&quot;&quot; & _ &quot; xmlns:c=&quot;&quot;urn:schemas:contacts:&quot;&quot;&quot; & _ &quot; xmlns:e=&quot;&quot;http://schemas.microsoft.com/exchange/&quot;&quot;&quot; & _ &quot; xmlns:mapi=&quot;&quot;http://schemas.microsoft.com/mapi/&quot;&quot;&quot; & _ &quot; xmlns:x=&quot;&quot;xml:&quot;&quot; xmlns:cal=&quot;&quot;urn:schemas:calendar:&quot;&quot;&quot; & _ &quot; xmlns:mail=&quot;&quot;urn:schemas:httpmail:&quot;&quot;>&quot;

' 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 = &quot;JoLynn&quot; & _ &quot;Julie</c:middlename>&quot; & _ &quot;<c:sn>Dobney</c:sn>&quot; & _ &quot;<c:cn>JoLynn J. Dobney</c:cn>&quot; & _ &quot;<mail:subject>JoLynn Dobney</mail:subject>&quot; & _ &quot;<c:fileas>Dobney, JoLynn</c:fileas>&quot; & _ &quot;<c:initials>JJD</c:initials>&quot; & _ &quot;<c:nickname>Jo</c:nickname>&quot; & _ &quot;<c:personaltitle>Mrs.</c:personaltitle>&quot; & _ &quot;<c:namesuffix>MCSD</c:namesuffix>&quot;

' Specify the business address information. ' (Street, PO Box, City, State, Postal Code, and Country) strBusinessAddrInfo = &quot;<c:street>Business Address Information</c:street>&quot; & _ &quot;<c:postofficebox>PO Box 12345</c:postofficebox>&quot; & _ &quot;<c:l>AnyTown</c:l>&quot; & _ &quot;<c:st>WA</c:st>&quot; & _ &quot;<c:postalcode>12345-1234</c:postalcode>&quot; & _ &quot;<c:co>United States</c:co>&quot;

' Specify the home address information. ' (Street, PO Box, City, State, Postal Code, and Country) strHomeAddrInfo = &quot;<c:homeStreet>Home Address Information</c:homeStreet>&quot; & _ &quot;<c:homepostofficebox>PO Box 54321</c:homepostofficebox>&quot; & _ &quot;<c:homeCity>SomeTown</c:homeCity>&quot; & _ &quot;<c:homeState>WA</c:homeState>&quot; & _ &quot;<c:homePostalCode>12345</c:homePostalCode>&quot; & _ &quot;<c:homeCountry>United States</c:homeCountry>&quot;

' Specify the other address information. ' (Street, PO Box, City, State, Postal Code, and Country) strOtherAddrInfo = &quot;<c:otherstreet>Other Address Information</c:otherstreet>&quot; & _ &quot;<c:otherpostofficebox>PO Box 98765</c:otherpostofficebox>&quot; & _ &quot;<c:othercity>SomeTown</c:othercity>&quot; & _ &quot;<c:otherstate>FL</c:otherstate>&quot; & _ &quot;<c:otherpostalcode>56789</c:otherpostalcode>&quot; & _ &quot;<c:othercountry>United States</c:othercountry>&quot;

' Specify the mailing address. ' 0 = None, 1 = Home, 2 = Business, 3 = Other strMailAddrInfo = &quot;<c:mailingaddressid>2</c:mailingaddressid>&quot;

' 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 = &quot;<c:telephoneNumber>425-555-0110</c:telephoneNumber>&quot; & _ &quot;<c:telephonenumber2>425-555-0111</c:telephonenumber2>&quot; & _ &quot;<c:facsimiletelephonenumber>425-555-0112</c:facsimiletelephonenumber>&quot; & _ &quot;<c:homePhone>425-555-0113</c:homePhone>&quot; & _ &quot;<c:homephone2>425-555-0114</c:homephone2>&quot; & _ &quot;<c:homefax>425-555-0115</c:homefax>&quot; & _ &quot;<c:otherTelephone>206-555-0111</c:otherTelephone>&quot; & _ &quot;<c:otherfax>206-555-0112</c:otherfax>&quot; & _ &quot;<c:pager>425-555-0116</c:pager>&quot; & _ &quot;<c:mobile>425-555-0117</c:mobile>&quot; & _ &quot;<c:othermobile>206-555-0113</c:othermobile>&quot; & _ &quot;<c:internationalisdnnumber>425-555-0118</c:internationalisdnnumber>&quot; & _ &quot;<c:telexnumber>425-555-0119</c:telexnumber>&quot; & _ &quot;<c:ttytddphone>425-555-0120</c:ttytddphone>&quot; & _ &quot;<c:callbackphone>425-555-0121</c:callbackphone>&quot;

' Specify the e-mail address information. strEmailInfo = &quot;<mapi:email1addrtype>EX</mapi:email1addrtype>&quot; & _ &quot;<mapi:email1emailaddress>&quot; & _ &quot;/o=Microsoft/ou=First Administrative Group/cn=Recipients/cn=jdobney&quot; & _ &quot;</mapi:email1emailaddress>&quot; & _ &quot;<mapi:email1originaldisplayname>&quot; & _ &quot;JoLynn Dobney (Exchange)&quot; & _ &quot;</mapi:email1originaldisplayname>&quot; & _ &quot;<mapi:email2addrtype>SMTP</mapi:email2addrtype>&quot; & _ &quot;<mapi:email2emailaddress>&quot; & _ &quot;JoLynn.Dobney@example.com&quot; & _ &quot;</mapi:email2emailaddress>&quot; & _ &quot;<mapi:email2originaldisplayname>&quot; & _ &quot;JoLynn Dobney (SMTP)&quot; & _ &quot;</mapi:email2originaldisplayname>&quot; & _ &quot;<mapi:email3addrtype>X400</mapi:email3addrtype>&quot; & _ &quot;<mapi:email3emailaddress>&quot; & _ &quot;c=us;a= ; p=Microsoft; o=Exchange; s=Dobney; g=JoLynn; i=J;&quot; & _ &quot;</mapi:email3emailaddress>&quot; & _ &quot;<mapi:email3originaldisplayname>&quot; & _ &quot;JoLynn Dobney (X400)&quot; & _ &quot;</mapi:email3originaldisplayname>&quot;

' 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 = &quot;<c:o>Fourth Coffee </c:o>&quot; & _ &quot;<c:organizationmainphone>425-555-0199</c:organizationmainphone>&quot; & _ &quot;<c:businesshomepage>http://www.fourthcoffee.com</c:businesshomepage>&quot; & _ &quot;<c:department>YYY</c:department>&quot; & _ &quot;<c:title>Lead Software Design Engineer</c:title>&quot; & _ &quot;<c:manager>Karan Khanna</c:manager>&quot; & _ &quot;<c:secretarycn>Ken Myer</c:secretarycn>&quot; & _ &quot;<c:secretaryphone>425-555-0123</c:secretaryphone>&quot; & _ &quot;<c:roomnumber>C-309</c:roomnumber>&quot; & _ &quot;<c:employeenumber>987654321</c:employeenumber>&quot; & _ &quot;<c:computernetworkname>jdobney</c:computernetworkname>&quot; & _ &quot;<c:profession>Software Designer</c:profession>&quot;

' Specify the personal information. ' (Birthday, Anniversary, Spouse, Children, Gender,       '  Personal Home Page, Hobbies) strPersonalInfo = &quot;<c:bday>1974-01-01T08:00:00Z</c:bday>&quot; & _ &quot;<c:weddinganniversary>1995-01-01T08:00:00Z</c:weddinganniversary>&quot; & _ &quot;<c:spousecn>Barry Johnson</c:spousecn>&quot; & _ &quot;<c:childrensnames>&quot; & _ &quot;<x:v>David Johnson</x:v><x:v>Willis Johnson</x:v>&quot; & _ &quot;</c:childrensnames>&quot; & _ &quot;<c:gender>Male</c:gender>&quot; & _ &quot;<c:personalHomePage>&quot; & _ &quot;http://www.example.com/JoLynnDobney/default.htm&quot; & _ &quot;</c:personalHomePage>&quot; & _ &quot;<c:hobbies>Reading, lawn bowling</c:hobbies>&quot;

' Specify the customer-related information. ' (Customer ID, Account, Billing Information) strCustomerInfo = &quot;<c:customerid>YYY</c:customerid>&quot; & _ &quot;<c:account>YYY</c:account>&quot; & _ &quot;<c:billinginformation>YYY</c:billinginformation>&quot;

' Specify follow-up information and reminder information. ' (Reminder, Reminder Topic, Reminder Time) strFollowUpInfo = &quot;<mapi:reminderset>1</mapi:reminderset>&quot; & _ &quot;<mapi:request>Call</mapi:request>&quot; & _ &quot;<mapi:remindertime>2001-12-01T08:00:00Z</mapi:remindertime>&quot; & _ &quot;<mapi:remindernexttime>2001-12-01T08:00:00Z</mapi:remindernexttime>&quot; & _ &quot;<e:reply-by-iso>2001-12-01T08:00:00Z</e:reply-by-iso>&quot;

' Specify miscellaneous information. ' (Categories, Contacts, Mileage, FTP Site, Language,       '  Government ID, Location, Internet Free/Busy Address,        '  Sensitivity) strMiscInfo = &quot;<e:keywords-utf8>&quot; & _ &quot;<x:v>Buddies</x:v><x:v>Engineers</x:v>&quot; & _ &quot;</e:keywords-utf8>&quot; & _ &quot;<mapi:contacts><x:v>Brian Johnson</x:v></mapi:contacts>&quot; & _ &quot;<e:mileage>Rarely used string property</e:mileage>&quot; & _ &quot;<c:ftpsite>ftp://ftp.example.com/</c:ftpsite>&quot; & _ &quot;<c:language>US English</c:language>&quot; & _ &quot;<c:governmentid>000-00-0000</c:governmentid>&quot; & _ &quot;<c:location>Nowhere Land</c:location>&quot; & _ &quot;<cal:fburl>http://www.example.com/JoLynnDobney/freebusy</cal:fburl>&quot; & _ &quot;<mapi:sensitivity>2</mapi:sensitivity>&quot;

' Specify the user field information. ' (User Field 1, User Field 2, User Field 3, User Field 4) strUserFieldsInfo = &quot;<e:extensionattribute1>User Data 1&quot; & _ &quot;</e:extensionattribute1>&quot; & _ &quot;<e:extensionattribute2>User Data 2</e:extensionattribute2>&quot; & _ &quot;<e:extensionattribute3>User Data 3</e:extensionattribute3>&quot; & _ &quot;<e:extensionattribute4>User Data 4</e:extensionattribute4>&quot;

Dim sQuery As String sQuery = &quot;<?xml version='1.0'?>&quot; & _ &quot;<g:propertyupdate &quot; & strXMLNSInfo & _ &quot;<g:set>&quot; & _ &quot;<g:prop>&quot; & _ &quot;<g:contentclass>urn:content-classes:person</g:contentclass>&quot; & _ &quot;<e:outlookmessageclass>IPM.Contact</e:outlookmessageclass>&quot; & _ strNameInfo & strBusinessAddrInfo & _ strHomeAddrInfo & strOtherAddrInfo & _ strMailAddrInfo & strPhoneInfo & _ strEmailInfo & strOrganizationalInfo & _ strPersonalInfo & strCustomerInfo & _ strFollowUpInfo & strMiscInfo & _ strUserFieldsInfo & _ &quot;</g:prop>&quot; & _ &quot;</g:set>&quot; & _ &quot;</g:propertyupdate>&quot;

' Set the credentials. ' TODO: Replace with the appropriate user credential Dim myCred As NetworkCredential = New NetworkCredential(&quot;Domain\UserName&quot;, &quot;Password&quot;) Dim MyCredentialCache As CredentialCache = New CredentialCache MyCredentialCache.Add(myUri, &quot;Basic&quot;, myCred) HttpWRequest.Credentials = MyCredentialCache

' Set the headers. HttpWRequest.KeepAlive = False HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;) HttpWRequest.Headers.Set(&quot;Translate&quot;, &quot;f&quot;) HttpWRequest.ContentType = &quot;text/xml&quot; HttpWRequest.ContentLength = sQuery.Length

'Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000 ' set the request method HttpWRequest.Method = &quot;PROPPATCH&quot;

' 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(&quot;Status: {0} {1}&quot;, sStatus, HttpWResponse.StatusDescription.ToString) Console.WriteLine(&quot;Request Headers:&quot;) Console.WriteLine(HttpWRequest.Headers.ToString) Console.WriteLine(&quot;Response Headers:&quot;) 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(&quot;Response: {0}&quot;, 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 </li> Search for the TODO text string in the code, and then modify the code for your environment.</li> Press the F5 key to build and to run the program.</li> Make sure that the Contact item is created.</li></ol>

Keywords: kbhowto kbxml KB314190

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.