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 Contact items in Visual C# on a computer that is running Exchange 2000 Server


View products that this article applies to.

This article was previously published under Q313119

↑ Back to the top


Summary

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

↑ Back to the top


More information

To create Contact items in Visual C#, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the code window, replace the code with the following:
    using System;
    using System.Net;
    using System.IO;         
     
    namespace WebDavNET
    {
       /// <summary>
       /// Summary description for Class1.
       /// </summary>
       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 = "http://ExchServer/Exchange/Administrator/Contacts/JoLynnDobney.eml";
    
                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 = "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 which address is the mailing address.
                // 0 = None, 1 = Home, 2 = Business, 3 = Other
                strMailAddrInfo = "<c:mailingaddressid>2</c:mailingaddressid>";
                // 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 = "<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 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 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 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/Reminder information.
                // (Reminder, Reminder Topic, Reminder Time, and other reminder information)
    		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>";
              
                string sQuery;
             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.
                NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
                CredentialCache myCredentialCache = 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.
                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("Status Code: {0}", 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("Response: {0}", 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("{0} Exception caught.", e);
             }
          }
       }
    
    }
    
  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 was created.

↑ Back to the top


Keywords: KB313119, kbhowto, kbxml, kbcode, kbmsg, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 313119
Revision : 7
Created on : 12/11/2006
Published on : 12/11/2006
Exists online : False
Views : 436