Microsoft KB Archive/313119

= How to create Contact items in Visual C# on a computer that is running Exchange 2000 Server =

Article ID: 313119

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition

-



This article was previously published under Q313119



SUMMARY
This article describes how to use the HttpWebRequest class and the HttpWebResponse class from the &quot;System.Net&quot; namespace to create Contact items in Microsoft Visual C# on a computer that is running Microsoft Exchange 2000 Server.



MORE INFORMATION
To create Contact items in Visual C#, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Console Application.

Note In Visual Studio 2005, click Console Application in the Visual C# list.

In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.  In the code window, replace the code with the following: using System; using System.Net; using System.IO; namespace WebDavNET {  ///    /// Summary description for Class1. ///   class Class1 {     static void Main(string[] args) {        try {           // TODO: Replace with the URL of an object on the computer that is running Exchange 2000. string sUri = &quot;http://ExchServer/Exchange/Administrator/Contacts/JoLynnDobney.eml&quot;;

System.Uri myUri = new System.Uri(sUri); HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

string strXMLNSInfo; string strNameInfo; string strBusinessAddrInfo; string strHomeAddrInfo; string strOtherAddrInfo; string strMailAddrInfo; string strPhoneInfo; string strEmailInfo; string strOrganizationalInfo;

string strPersonalInfo; string strCustomerInfo; string strFollowUpInfo; string strMiscInfo; string strUserFieldsInfo;

// Specify the Namespaces to be used. strXMLNSInfo = &quot;xmlns:g=\&quot;DAV:\&quot;&quot; + &quot; xmlns:c=\&quot;urn:schemas:contacts:\&quot;&quot; + &quot; xmlns:e=\&quot;http://schemas.microsoft.com/exchange/\&quot;&quot; + &quot; xmlns:mapi=\&quot;http://schemas.microsoft.com/mapi/\&quot;&quot; + &quot; xmlns:x=\&quot;xml:\&quot; xmlns:cal=\&quot;urn:schemas:calendar:\&quot;&quot; + &quot; xmlns:mail=\&quot;urn:schemas:httpmail:\&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&quot; + &quot;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 which address is the mailing address. // 0 = None, 1 = Home, 2 = Business, 3 = Other strMailAddrInfo = &quot;<c:mailingaddressid>2</c:mailingaddressid>&quot;; // Specify 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 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 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 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/Reminder information. // (Reminder, Reminder Topic, Reminder Time, and other reminder information) 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;; string sQuery; 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. NetworkCredential myCred = new NetworkCredential(@&quot;DomainName\UserName&quot;, &quot;UserPassword&quot;); CredentialCache myCredentialCache = 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. byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery); HttpWRequest.ContentLength = ByteQuery.Length; Stream QueryStream = 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, HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse;

// Get the Status code int iStatCode = (int)HttpWResponse.StatusCode; string sStatus = iStatCode.ToString; Console.WriteLine(&quot;Status Code: {0}&quot;, sStatus); // Get the request headers string sReqHeaders = HttpWRequest.Headers.ToString; Console.WriteLine(sReqHeaders);

// Read the Response Steam Stream strm = HttpWResponse.GetResponseStream; StreamReader sr = new StreamReader(strm); string sText = sr.ReadToEnd; Console.WriteLine(&quot;Response: {0}&quot;, sText);

// Close the stream. strm.Close;

// Clean up           myCred = null; myCredentialCache = null; HttpWRequest = null; HttpWResponse = null; QueryStream = null; strm = null; sr = null; }        catch (Exception e)         { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }     }   }

} </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 was created.</li></ol>

Additional query words: create, contact, items

Keywords: kbhowtomaster kbmsg kbcode kbxml kbhowto KB313119

-

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

© Microsoft Corporation. All rights reserved.