/// <summary>
 /// Constructor
 /// </summary>
 public AppointmentSender()
     : base()
 {
     AppointmentInfo = new VCalendar();
 }
Exemple #2
0
        /// <summary>
        /// Returns a list of the user's calendar items.
        /// </summary>
        /// <param name="UserName">User name used for logging in</param>
        /// <param name="Password">Password for logging in</param>
        /// <param name="Directory">User's directory</param>
        /// <param name="StartDate">Date to start at</param>
        /// <param name="EndDate">Date to end at</param>
        /// <param name="Server">Server Name</param>
        /// <returns>A list of the user's calendar items in VCalendar format, between two date ranges</returns>
        public static List<VCalendar> GetCalendarItems(string UserName, string Password, string Server, string Directory, DateTime StartDate, DateTime EndDate)
        {
            List<VCalendar> ReturnArray = new List<VCalendar>();
            string Uri = string.Format("http://{0}/exchange/{1}/calendar/", Server, Directory);

            string Query = "<?xml version=\"1.0\"?>"
                 + "<g:searchrequest xmlns:g=\"DAV:\">"
                 + "<g:sql>SELECT \"urn:schemas:calendar:location\", \"urn:schemas:httpmail:subject\", "
                 + "\"urn:schemas:httpmail:textdescription\", "
                 + "\"urn:schemas:calendar:dtstart\", \"urn:schemas:calendar:dtend\", "
                 + "\"urn:schemas:calendar:busystatus\", \"urn:schemas:calendar:instancetype\" "
                 + "FROM Scope('SHALLOW TRAVERSAL OF \"" + Uri + "\"') "
                 + "WHERE ((\"urn:schemas:calendar:dtstart\" &gt;= '" + StartDate.ToString("yyyy/MM/dd hh:mm:ss") + "' "
                 + "AND \"urn:schemas:calendar:dtstart\" &lt;= '" + EndDate.ToString("yyyy/MM/dd hh:mm:ss") + "') "
                 + "OR (\"urn:schemas:calendar:dtend\" &gt;= '" + StartDate.ToString("yyyy/MM/dd hh:mm:ss") + "' "
                 + "AND \"urn:schemas:calendar:dtstart\" &lt;= '" + EndDate.ToString("yyyy/MM/dd hh:mm:ss") + "')) "
                 + "AND \"DAV:contentclass\" = 'urn:content-classes:appointment' "
                 + "AND NOT \"urn:schemas:calendar:instancetype\" = 1 "
                 + "ORDER BY \"urn:schemas:calendar:dtstart\" ASC"
                 + "</g:sql></g:searchrequest>";
            byte[] Contents = System.Text.Encoding.UTF8.GetBytes(Query);

            HttpWebRequest Request = HttpWebRequest.Create(Uri) as HttpWebRequest;
            System.Net.CredentialCache MyCredentialCache = new System.Net.CredentialCache();
            if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
            {
                MyCredentialCache.Add(new System.Uri(Uri),
                                   "NTLM",
                                   new System.Net.NetworkCredential(UserName, Password));
            }
            else
            {
                MyCredentialCache.Add(new System.Uri(Uri),
                                       "Negotiate",
                                       (System.Net.NetworkCredential)CredentialCache.DefaultCredentials);
            }
            Request.Credentials = MyCredentialCache;
            Request.Method = "SEARCH";
            Request.ContentLength = Contents.Length;
            Request.ContentType = "text/xml";

            using (System.IO.Stream RequestStream = Request.GetRequestStream())
            {
                RequestStream.Write(Contents, 0, Contents.Length);

                using (HttpWebResponse Response = Request.GetResponse() as HttpWebResponse)
                {
                    using (System.IO.Stream ResponseStream = Response.GetResponseStream())
                    {
                        XmlDocument Document = new XmlDocument();
                        Document.Load(ResponseStream);
                        foreach (XmlElement Element in Document.GetElementsByTagName("a:prop"))
                        {
                            VCalendar Cal = new VCalendar();
                            if (Element["e:textdescription"] != null)
                            {
                                Cal.Description = Element["e:textdescription"].InnerText;
                            }
                            if (Element["e:subject"] != null)
                            {
                                Cal.Subject = Element["e:subject"].InnerText;
                            }
                            if (Element["d:location"] != null)
                            {
                                Cal.Location = Element["d:location"].InnerText;
                            }
                            if (Element["d:dtstart"] != null)
                            {
                                Cal.StartTime = DateTime.Parse(Element["d:dtstart"].InnerText);
                            }
                            if (Element["d:dtend"] != null)
                            {
                                Cal.EndTime = DateTime.Parse(Element["d:dtend"].InnerText);
                            }
                            ReturnArray.Add(Cal);
                        }
                    }
                }
            }
            return ReturnArray;
        }