Пример #1
0
        public bool CancelAppointment(HackExchangeContext context, CalendarItem appointment)
        {
            var url = context.Endpoint;
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var postBodyTemplate = LoadXml("CancelAppointment");
            var postBody = string.Format(postBodyTemplate, appointment.Id, appointment.ChangeKey);
            request.Content = new StringContent(postBody, Encoding.UTF8, "text/xml");

            var clientHandler = new HttpClientHandler()
            {
                Credentials = context.Credentials
            };
            using (var client = new HttpClient(clientHandler))
            {
                var response = client.SendAsync(request).Result;
                var responseBody = response.Content.ReadAsStringAsync().Result;

                var doc = new XPathDocument(new StringReader(responseBody));
                var nav = doc.CreateNavigator();
                var nsManager = new XmlNamespaceManager(nav.NameTable);
                nsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
                nsManager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

                var responseClass = EvaluateXPath(nav, nsManager, "//m:DeleteItemResponseMessage/@ResponseClass");
                return responseClass == "Success";
            }
        }
Пример #2
0
        public CalendarItem CreateAppointment(HackExchangeContext context, string subject, string description, DateTime startTime, DateTime endTime, Room room, List<string> requiredAttendees)
        {
            var url = context.Endpoint;
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var postBodyTemplate = LoadXml("CreateAppointment");
            var attendeeTemplate = LoadXml("AttendeeTemplate");

            var roomAttendeeBody = string.Format(attendeeTemplate, room.EmailAddress);
            string requiredAttendeesBody = "";
            if (requiredAttendees != null)
            {
                var attendeesBuilder = new StringBuilder();
                foreach (var attendee in requiredAttendees)
                {
                    attendeesBuilder.AppendFormat(attendeeTemplate, attendee);
                    attendeesBuilder.AppendLine();
                }
                requiredAttendeesBody = attendeesBuilder.Length > 0 ? attendeesBuilder.ToString() : "";
            }

            var startTimeBody = string.Format("{0:yyyy-MM-ddTHH:mm:ssZ}", startTime.ToUniversalTime());
            var endTimeBody = string.Format("{0:yyyy-MM-ddTHH:mm:ssZ}", endTime.ToUniversalTime());

            var postBody = string.Format(postBodyTemplate, subject, description, startTimeBody, endTimeBody, room.Name, requiredAttendeesBody, roomAttendeeBody);

            request.Content = new StringContent(postBody, Encoding.UTF8, "text/xml");

            var clientHandler = new HttpClientHandler()
            {
                Credentials = context.Credentials
            };
            using (var client = new HttpClient(clientHandler))
            {
                var response = client.SendAsync(request).Result;
                var responseBody = response.Content.ReadAsStringAsync().Result;

                var doc = new XPathDocument(new StringReader(responseBody));
                var nav = doc.CreateNavigator();
                var nsManager = new XmlNamespaceManager(nav.NameTable);
                nsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
                nsManager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

                CalendarItem calendarItem = null;
                var calendarItemNode = nav.SelectSingleNode("//t:CalendarItem/t:ItemId", nsManager);
                if (calendarItemNode != null)
                {
                    var id = EvaluateXPath(calendarItemNode, nsManager, "@Id");
                    var changeKey = EvaluateXPath(calendarItemNode, nsManager, "@ChangeKey");
                    if (id != null && changeKey != null)
                    {
                        calendarItem = new CalendarItem()
                        {
                            Id = id,
                            ChangeKey = changeKey
                        };
                    }
                }
                return calendarItem;
            }
        }
Пример #3
0
        public CalendarItem GetCalendarFolder(HackExchangeContext context)
        {
            var url = context.Endpoint;
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var postBody = LoadXml("GetCalendarFolder");

            request.Content = new StringContent(postBody, Encoding.UTF8, "text/xml");

            var clientHandler = new HttpClientHandler()
            {
                Credentials = context.Credentials
            };
            using (var client = new HttpClient(clientHandler))
            {
                var response = client.SendAsync(request).Result;
                var responseBody = response.Content.ReadAsStringAsync().Result;

                var doc = new XPathDocument(new StringReader(responseBody));
                var nav = doc.CreateNavigator();
                var nsManager = new XmlNamespaceManager(nav.NameTable);
                nsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
                nsManager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

                CalendarItem calendarFolder = null;
                var folderIdNode = nav.SelectSingleNode("//t:FolderId", nsManager);
                if (folderIdNode != null)
                {
                    var id = EvaluateXPath(folderIdNode, nsManager, "@Id");
                    var changeKey = EvaluateXPath(folderIdNode, nsManager, "@ChangeKey");
                    if (id != null && changeKey != null)
                    {
                        calendarFolder = new CalendarItem()
                        {
                            Id = id,
                            ChangeKey = changeKey
                        };
                    }
                }
                return calendarFolder;
            }
        }
Пример #4
0
        public IList<CalendarItem> GetAppointments(HackExchangeContext context)
        {
            var calendarFolder = GetCalendarFolder(context);
            var url = context.Endpoint;
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var postBodyTemplate = LoadXml("GetAppointments");

            var now = DateTime.Now;
            var startTimeBody = string.Format("{0:yyyy-MM-ddTHH:mm:ssZ}", now.Date.ToUniversalTime());
            var endTimeBody = string.Format("{0:yyyy-MM-ddTHH:mm:ssZ}", now.Date.AddDays(1).ToUniversalTime());
            var postBody = string.Format(postBodyTemplate, startTimeBody, endTimeBody, calendarFolder.Id, calendarFolder.ChangeKey);
            request.Content = new StringContent(postBody, Encoding.UTF8, "text/xml");

            var clientHandler = new HttpClientHandler()
            {
                Credentials = context.Credentials
            };
            using (var client = new HttpClient(clientHandler))
            {
                var response = client.SendAsync(request).Result;
                var responseBody = response.Content.ReadAsStringAsync().Result;

                var doc = new XPathDocument(new StringReader(responseBody));
                var nav = doc.CreateNavigator();
                var nsManager = new XmlNamespaceManager(nav.NameTable);
                nsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
                nsManager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

                var calendarItems = new List<CalendarItem>();
                var calendarItemsIterator = nav.Select("//t:CalendarItem", nsManager);
                foreach (XPathNavigator calendarItemNav in calendarItemsIterator)
                {
                    var id = EvaluateXPath(calendarItemNav, nsManager, "t:ItemId/@Id");
                    var changeKey = EvaluateXPath(calendarItemNav, nsManager, "t:ItemId/@ChangeKey");
                    var subject = EvaluateXPath(calendarItemNav, nsManager, "t:Subject");
                    var location = EvaluateXPath(calendarItemNav, nsManager, "t:Location");

                    if (id != null && changeKey != null)
                    {
                        var calendarItem = new CalendarItem()
                        {
                            Id = id,
                            ChangeKey = changeKey,
                            Subject = subject,
                            Location = location
                        };
                        calendarItems.Add(calendarItem);
                    }
                }
                return calendarItems;
            }
        }