Exchange Server: How to retrieve appointments using C# & WebDAV?
Code Snippet (C#):
//Declaration part
string strExchSvrName = "";
string strMailbox = "";
string strCalendarUri = "";
string strDomain = "";
string strUserName = "";
string strPassword = "";
System.Net.HttpWebRequest WebDavRequest = null;
System.Net.HttpWebResponse WebDavResponse = null;
System.Net.CredentialCache MyCredentialCache = null;
byte[] bytes = null;
System.IO.Stream WebDavRequestStream = null;
// Provide the Exchange server name;
strExchSvrName = "mydomain.in";
// Provide Mailbox folder name.
strMailbox = "Mailbox1";
//if HTTPS -then provide URI of the user's calendar folder
strCalendarUri = "https://" + strExchSvrName + "/exchange/"+ strMailbox + "/Calendar/";
//if HTTP -then provide URI of the user's calendar folder
//strCalendarUri = "https://" + strExchSvrName + "/exchange/" + strMailbox + "/Calendar/";
//Provide the User name and password of appointment creator. Make sure the user has enough permissions to read
strUserName = "username";
strDomain = "domain";
strPassword = "password";
//Build the Query
string query = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT \"https://schemas.microsoft.com/exchange/outlookmessageclass\", \"DAV:contentclass\",
\"DAV:displayname\" FROM \"" + strCalendarUri + "\""
+ "WHERE \"DAV:ishidden\" = true AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";
query = "<?xml version=\"1.0\"?>"
+ "<dav:searchrequest xmlns:dav=\"DAV:\">"
+ "<dav:sql>"
+ "SELECT "
+ "\"DAV:displayname\", " // Appointment Uri portion of the resource
+ "\"DAV:href\", " // Full resource Uri of the appointment
+ "\"urn:schemas:httpmail:subject\", " // Subject of appointment
+ "\"urn:schemas:calendar:dtstart\", " // Start date/time of appointment
+ "\"urn:schemas:calendar:dtend\", " // End date/time of appointment
+ "\"urn:schemas:httpmail:textdescription\", " // Body of appointment
+ "\"urn:schemas:calendar:location\", " // Location of appointment
+ "\"urn:schemas:calendar:alldayevent\", " // Whether appointments an all day appointment
+ "\"urn:schemas:calendar:meetingstatus\", " // Confimed status of the appointment
+ "\"urn:schemas:calendar:busystatus\", " // Comitted status the appointment represents
+ "\"https://schemas.microsoft.com/mapi/proptag/x823D0003\", " // Color of the appointment
+ "\"urn:schemas:calendar:reminderoffset\", " // Reminder offset of the appointment
+ "\"DAV:ishidden\", " // Whether this is hidden
+ "\"urn:schemas:calendar:instancetype\", " // Relation of the appointment to a recurring series
+ "\"urn:schemas:calendar:transparent\", " // Transparency of the appointment to free/busy searches
+ "\"urn:schemas:calendar:timezoneid\" " // Display timezone of the appointment
+ "FROM Scope('SHALLOW TRAVERSAL OF \"" + strCalendarUri + "\"') "
+ "WHERE "
+ "(\"DAV:contentclass\" = 'urn:content-classes:appointment' ) " // Item is an appointment
+ "AND (NOT \"urn:schemas:calendar:instancetype\" = 1) " // appointment is not the master of a recurring series (Get single
appointments or instances of recurring appointments)
+ "AND (\"urn:schemas:calendar:dtend\" > '" +
Convert.ToDateTime("3-12-2007").Date.ToUniversalTime().ToString("yyyy/MM/dd HH:mm:ss") + "')
" //Appointment ends after the start of our range
+ "AND (\"urn:schemas:calendar:dtstart\" < '" +
Convert.ToDateTime("3-17-2007").Date.ToUniversalTime().ToString("yyyy/MM/dd HH:mm:ss") + "')
" //Appointment begins before the end of our range
+ "AND (\"DAV:displayname\" like '" + "LS.ImportantDates." + "%' ) "
+ "ORDER BY \"urn:schemas:calendar:dtstart\" "
+ "</dav:sql>"
+ "</dav:searchrequest>";
//Provide credentials required to access the server. Here i used the NTLM
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(strCalendarUri), "NTLM", new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
//Create the HttpWebRequest object.
WebDavRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strCalendarUri);
// Add the network credentials to the request.
WebDavRequest.Credentials = MyCredentialCache;
// Specify the PROPPATCH method.
WebDavRequest.Method = "SEARCH";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes(query);
// Set the content header length.
WebDavRequest.ContentLength = bytes.Length;
// Get a reference to the request stream.
WebDavRequestStream = WebDavRequest.GetRequestStream();
// Write the message body to the request stream.
WebDavRequestStream.Write(bytes, 0, bytes.Length);
// Release the connection
WebDavRequestStream.Close();
// Set the content type header.
WebDavRequest.ContentType = "text/xml";
WebDavResponse = (System.Net.HttpWebResponse)WebDavRequest.GetResponse();
Response.Write("Calendar : ");
Response.Write("Status Code: " + WebDavResponse.StatusCode + "Description: " + WebDavResponse.StatusDescription);
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(WebDavResponse.GetResponseStream()))
{
Response.Write(streamReader.ReadToEnd());
}
WebDavResponse.Close();
You can get more related samples and detailed information from Dan's blog. For more information you can also refer this article .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Comments
- Anonymous
June 16, 2009
PingBack from http://topalternativedating.info/story.php?id=7018