Microsoft KB Archive/313120

= How to use the HttpWebRequest class and the HttpWebResponse class to create appointment items on a computer that is running Exchange 2000 Server by using Visual C# =

Article ID: 313120

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 Q313120



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



MORE INFORMATION
To create an appointment item on a computer that is running Exchange 2000 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 Exchange Server string sUri = &quot;http://ExchServer/Exchange/Administrator/Calendar/testappt.eml&quot;;

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

string strXMLNSInfo = &quot;xmlns:g=\&quot;DAV:\&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:dt=\&quot;urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\&quot;&quot; + &quot; xmlns:mail=\&quot;urn:schemas:httpmail:\&quot;>&quot;;

string sQuery = &quot;&quot; + &quot;&quot; + &quot;&quot; + &quot;urn:content-classes:appointment&quot; + &quot;<e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>&quot; + &quot;<mail:subject>Appointment Subject</mail:subject>&quot; + &quot;<cal:location>Appointment Location</cal:location>&quot; + &quot;<cal:dtstart dt:dt=\&quot;dateTime.tz\&quot;>2001-12-27T22:00:00.000Z</cal:dtstart>&quot; + &quot;<cal:dtend dt:dt=\&quot;dateTime.tz\&quot;>2001-12-27T22:30:00.000Z</cal:dtend>&quot; + &quot;<cal:instancetype dt:dt=\&quot;int\&quot;>0</cal:instancetype>&quot; + &quot;<cal:busystatus>BUSY</cal:busystatus>&quot; + &quot;<cal:meetingstatus>TENTATIVE</cal:meetingstatus>&quot; + &quot;<cal:alldayevent dt:dt=\&quot;boolean\&quot;>0</cal:alldayevent>&quot; + &quot;</g:prop>&quot; + &quot;</g:set>&quot; + &quot;</g:propertyupdate>&quot;;

// Set credentials. // TODO: Replace with 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 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 stream. 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 TODO in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Make sure that the appointment item was created.</li></ol>

Additional query words: create, appointment, items

Keywords: kbhowtomaster kbmsg kbxml kbcode kbhowto KB313120

-

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

© Microsoft Corporation. All rights reserved.