Creating an Appointment (WebDAV)
Topic Last Modified: 2006-06-12
This topic shows how to create an appointment in a user's calendar using the WebDAV PROPPATCH Method.
Note
You can only add an appointment to a user's calendar folder that you have permissions to write to.
See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.
This topic contains Microsoft® Visual Basic®, Microsoft C#, and Visual Basic .NET code examples.
Example
Visual Basic
Example
Option Explicit
Private Sub CreateAppointment()
On Error GoTo ErrHandler
' Variables
Dim strApptURL As String
Dim strExchSvrName As String
Dim strMailbox As String
Dim strApptItem As String
Dim strUserName As String
Dim strPassWord As String
Dim strApptRequest As String
Dim strCalInfo As String
Dim strHeaderInfo As String
Dim strMailInfo As String
Dim strXMLNSInfo As String
' Use MSXML 4.0
Dim xmlReq As MSXML2.XMLHTTP40
' Exchange server name.
strExchSvrName = "ExchangeServer"
' Mailbox folder name.
strMailbox = "user"
' Appointment item.
strApptItem = "testappointment.eml"
' Username and password of appointment creator.
strUserName = "Domain\user"
strPassWord = "!Password"
' URL of the appointment item.
strApptURL = "http://" & strExchSvrName & "/exchange/" & _
strMailbox & "/Calendar/" & strApptItem
' XML namespace info of the WebDAV request.
strXMLNSInfo = "xmlns:g=""DAV:"" " & _
"xmlns:e=""https://schemas.microsoft.com/exchange/"" " & _
"xmlns:mapi=""https://schemas.microsoft.com/mapi/"" " & _
"xmlns:mapit=""https://schemas.microsoft.com/mapi/proptag/"" " & _
"xmlns:x=""xml:"" xmlns:cal=""urn:schemas:calendar:"" " & _
"xmlns:dt=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " & _
"xmlns:header=""urn:schemas:mailheader:"" " & _
"xmlns:mail=""urn:schemas:httpmail:"">"
' Set the appointment item properties. The reminder time is set in seconds.
' To create an all-day meeting, set the dtstart/dtend range for 24 hours
' or more and set the alldayevent property to 1. See the documentation
' on the properties in the urn:schemas:calendar: namespace for more information.
strCalInfo = "<cal:location>meetappt Location</cal:location>" & _
"<cal:dtstart dt:dt=""dateTime.tz"">2004-05-13T23:00:00.000Z</cal:dtstart>" & _
"<cal:dtend dt:dt=""dateTime.tz"">2004-05-13T23:30:00.000Z</cal:dtend>" & _
"<cal:instancetype dt:dt=""int"">0</cal:instancetype>" & _
"<cal:busystatus>BUSY</cal:busystatus>" & _
"<cal:meetingstatus>CONFIRMED</cal:meetingstatus>" & _
"<cal:alldayevent dt:dt=""boolean"">0</cal:alldayevent>" & _
"<cal:responserequested dt:dt=""boolean"">1</cal:responserequested>" & _
"<cal:reminderoffset dt:dt=""int"">900</cal:reminderoffset>"
' Set the required attendee of the appointment.
strHeaderInfo = "<header:to>" & strMailbox & "</header:to>"
' Set the subject of the appointment.
strMailInfo = "<mail:subject>Test Appointment Subject</mail:subject>" & _
"<mail:htmldescription>Let's meet here</mail:htmldescription>"
' Build the XML body of the PROPPATCH request.
strApptRequest = "<?xml version=""1.0""?>" & _
"<g:propertyupdate " & strXMLNSInfo & _
"<g:set><g:prop>" & _
"<g:contentclass>urn:content-classes:appointment</g:contentclass>" & _
"<e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>" & _
strMailInfo & _
strCalInfo & _
strHeaderInfo & _
"<mapi:finvited dt:dt=""boolean"">1</mapi:finvited>" & _
"</g:prop></g:set>" & _
"</g:propertyupdate>"
' Create the DAV PROPPATCH request.
Set xmlReq = CreateObject("Microsoft.XMLHTTP")
xmlReq.open "PROPPATCH", strApptURL, False, strUserName, strPassWord
xmlReq.setRequestHeader "Content-Type", "text/xml"
xmlReq.send strApptRequest
' The PROPPATCH request was successful.
If (xmlReq.Status >= 200 And xmlReq.Status < 300) Then
MsgBox "Appointment created successfully."
' Display error info.
Else
MsgBox "PROPPATCH status: " & xmlReq.Status & vbCrLf & _
"Status text: " & xmlReq.statusText
End If
' Clean up.
Set xmlReq = Nothing
Exit Sub
' Implement custom error handling here.
ErrHandler:
' Display error info.
MsgBox "Error creating appointment/meeting: " & Err.Number & ": " & Err.Description
' Clean up.
Set xmlReq = Nothing
End Sub
C#
Example
using System;
using System.Net;
using System.Text;
namespace ExchangeSDK.Snippets.CSharp
{
class AppointmentCreatorWebDAV
{
[STAThread]
static void Main(string[] args)
{
// Variables.
string strExchSvrName = "";
string strMailbox = "";
string strCalendarUri = "";
string strApptItem = "";
string strDomain = "";
string strUserName = "";
string strPassword = "";
string strApptRequest = "";
string strMailInfo = "";
string strCalInfo = "";
string strXMLNSInfo = "";
string strHeaderInfo = "";
System.Net.HttpWebRequest PROPPATCHRequest = null;
System.Net.WebResponse PROPPATCHResponse = null;
System.Net.CredentialCache MyCredentialCache = null;
byte[] bytes = null;
System.IO.Stream PROPPATCHRequestStream = null;
try
{
// Exchange server name;
strExchSvrName = "ExchangeServer";
// Mailbox folder name.
strMailbox = "user";
// Appointment item.
strApptItem = "testappointment.eml";
// URI of the user's calendar folder.
strCalendarUri = "http://" + strExchSvrName + "/exchange/"
+ strMailbox + "/Calendar/";
// User name and password of appointment creator.
strUserName = "user";
strDomain = "Domain";
strPassword = "!Password";
// XML namespace info for the WebDAV request.
strXMLNSInfo = "xmlns:g=\"DAV:\" "
+ "xmlns:e=\"https://schemas.microsoft.com/exchange/\" "
+ "xmlns:mapi=\"https://schemas.microsoft.com/mapi/\" "
+ "xmlns:mapit=\"https://schemas.microsoft.com/mapi/proptag/\" "
+ "xmlns:x=\"xml:\" xmlns:cal=\"urn:schemas:calendar:\" "
+ "xmlns:dt=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\" "
+ "xmlns:header=\"urn:schemas:mailheader:\" "
+ "xmlns:mail=\"urn:schemas:httpmail:\"";
// Set the appointment item properties. To create an all-day meeting,
// set the dtstart/dtend range for 24 hours or more and set the alldayevent property
// to 1. See the documentation on the properties
// in the urn:schemas:calendar: namespace for more information.
strCalInfo = "<cal:location>meetappt Location</cal:location>"
+ "<cal:dtstart dt:dt=\"dateTime.tz\">2004-05-18T23:00:00.000Z</cal:dtstart>"
+ "<cal:dtend dt:dt=\"dateTime.tz\">2004-05-18T23:30:00.000Z</cal:dtend>"
+ "<cal:instancetype dt:dt=\"int\">0</cal:instancetype>"
+ "<cal:busystatus>BUSY</cal:busystatus>"
+ "<cal:meetingstatus>CONFIRMED</cal:meetingstatus>"
+ "<cal:alldayevent dt:dt=\"boolean\">0</cal:alldayevent>"
+ "<cal:responserequested dt:dt=\"boolean\">1</cal:responserequested>"
// Set the reminder time (in seconds).
+ "<cal:reminderoffset dt:dt=\"int\">900</cal:reminderoffset>";
// Set the required attendee of the appointment.
strHeaderInfo = "<header:to>" + strMailbox + "</header:to>";
// Set the subject of the appointment.
strMailInfo = "<mail:subject>Test Appointment Subject</mail:subject>"
+ "<mail:htmldescription>Let's meet here</mail:htmldescription>";
// Build the XML body of the PROPPATCH request.
strApptRequest = "<?xml version=\"1.0\"?>"
+ "<g:propertyupdate " + strXMLNSInfo + ">"
+ "<g:set><g:prop>"
+ "<g:contentclass>urn:content-classes:appointment</g:contentclass>"
+ "<e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>"
+ strMailInfo
+ strCalInfo
+ strHeaderInfo
+ "<mapi:finvited dt:dt=\"boolean\">1</mapi:finvited>"
+ "</g:prop></g:set>"
+ "</g:propertyupdate>";
// Create a new CredentialCache object and fill it with the network
// credentials required to access the server.
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add( new System.Uri(strCalendarUri),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);
// Create the HttpWebRequest object.
PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strCalendarUri + strApptItem);
// Add the network credentials to the request.
PROPPATCHRequest.Credentials = MyCredentialCache;
// Specify the PROPPATCH method.
PROPPATCHRequest.Method = "PROPPATCH";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)strApptRequest);
// Set the content header length. This must be
// done before writing data to the request stream.
PROPPATCHRequest.ContentLength = bytes.Length;
// Get a reference to the request stream.
PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
// Write the message body to the request stream.
PROPPATCHRequestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection
// for further use.
PROPPATCHRequestStream.Close();
// Set the content type header.
PROPPATCHRequest.ContentType = "text/xml";
// Create the appointment in the Calendar folder of the
// user's mailbox.
PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
// Clean up.
PROPPATCHResponse.Close();
Console.WriteLine("Appointment successfully created.");
}
catch(Exception ex)
{
// Catch any exceptions. Any error codes from the PROPPATCH
// method request on the server will be caught
// here, also.
Console.WriteLine(ex.Message);
}
}
}
}
Visual Basic .NET
Example
Option Explicit On
Option Strict On
Module Module1
Sub Main()
' Variables
Dim strExchSvrName As String
Dim strMailbox As String
Dim strCalendarUri As String
Dim strApptItem As String
Dim strDomain As String
Dim strUserName As String
Dim strPassword As String
Dim strApptRequest As String
Dim strMailInfo As String
Dim strCalInfo As String
Dim strXMLNSInfo As String
Dim strHeaderInfo As String
Dim PROPPATCHRequest As System.Net.HttpWebRequest
Dim PROPPATCHResponse As System.Net.WebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim bytes() As Byte
Dim PROPPATCHRequestStream As System.IO.Stream
Try
' Exchange server name
strExchSvrName = "ExchangeServer"
' Mailbox folder name.
strMailbox = "user"
' Appointment item.
strApptItem = "testappointment.eml"
' URI of the user's calendar folder.
strCalendarUri = "http://" & strExchSvrName & "/exchange/" & _
strMailbox & "/Calendar/"
' User name and password of appointment creator.
strUserName = "user"
strDomain = "Domain"
strPassword = "!Password"
' XML namespace info for the WebDAV request.
strXMLNSInfo = "xmlns:g=""DAV:"" " & _
"xmlns:e=""https://schemas.microsoft.com/exchange/"" " & _
"xmlns:mapi=""https://schemas.microsoft.com/mapi/"" " & _
"xmlns:mapit=""https://schemas.microsoft.com/mapi/proptag/"" " & _
"xmlns:x=""xml:"" xmlns:cal=""urn:schemas:calendar:"" " & _
"xmlns:dt=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " & _
"xmlns:header=""urn:schemas:mailheader:"" " & _
"xmlns:mail=""urn:schemas:httpmail:"""
' Set the appointment item properties. The reminder time is set in seconds.
' To create an all-day meeting, set the dtstart/dtend range for 24 hours
' or more and set the alldayevent property to 1. See the documentation
' on the properties in the urn:schemas:calendar: namespace for more information.
strCalInfo = "<cal:location>meetappt Location</cal:location>" & _
"<cal:dtstart dt:dt=""dateTime.tz"">2004-05-18T23:00:00.000Z</cal:dtstart>" & _
"<cal:dtend dt:dt=""dateTime.tz"">2004-05-18T23:30:00.000Z</cal:dtend>" & _
"<cal:instancetype dt:dt=""int"">0</cal:instancetype>" & _
"<cal:busystatus>BUSY</cal:busystatus>" & _
"<cal:meetingstatus>CONFIRMED</cal:meetingstatus>" & _
"<cal:alldayevent dt:dt=""boolean"">0</cal:alldayevent>" & _
"<cal:responserequested dt:dt=""boolean"">1</cal:responserequested>" & _
"<cal:reminderoffset dt:dt=""int"">900</cal:reminderoffset>"
' Set the required attendee of the appointment.
strHeaderInfo = "<header:to>" & strMailbox & "</header:to>"
' Set the subject of the appointment.
strMailInfo = "<mail:subject>Test Appointment Subject</mail:subject>" & _
"<mail:htmldescription>Let's meet here</mail:htmldescription>"
' Build the XML body of the PROPPATCH request.
strApptRequest = "<?xml version=""1.0""?>" & _
"<g:propertyupdate " & strXMLNSInfo & ">" & _
"<g:set><g:prop>" & _
"<g:contentclass>urn:content-classes:appointment</g:contentclass>" & _
"<e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>" & _
strMailInfo & _
strCalInfo & _
strHeaderInfo & _
"<mapi:finvited dt:dt=""boolean"">1</mapi:finvited>" & _
"</g:prop></g:set>" & _
"</g:propertyupdate>"
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strCalendarUri), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the HttpWebRequest object.
PROPPATCHRequest = CType(System.Net.HttpWebRequest.Create(strCalendarUri & strApptItem), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
PROPPATCHRequest.Credentials = MyCredentialCache
' Specify the PROPPATCH method.
PROPPATCHRequest.Method = "PROPPATCH"
' Set the content type header.
PROPPATCHRequest.ContentType = "text/xml"
' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(strApptRequest)
' Set the content header length. This must be
' done before writing data to the request stream.
PROPPATCHRequest.ContentLength = bytes.Length
' Get a reference to the request stream.
PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream()
' Write the message body to the request stream.
PROPPATCHRequestStream.Write(bytes, 0, bytes.Length)
' Close the Stream object to release the connection
' for further use.
PROPPATCHRequestStream.Close()
' Create the appointment in the Calendar folder of the
' user's mailbox.
PROPPATCHResponse = CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)
Console.WriteLine("Appointment successfully created.")
' Clean up.
PROPPATCHResponse.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the PROPPATCH
' or MOVE method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module