public async Task <CalendarEvent> AddOrUpdateCalendarEvent(CalendarEvent calendarEvent)
        {
            // Query to see if event already exists. If so, do Update instead.
            string[] queryParameters =
            {
                String.Format(CultureInfo.InvariantCulture, "$filter=contains(Subject,'Spid:[{0}]')",
                              calendarEvent.Spid),
            };

            string requestUrl = String.Format(CultureInfo.InvariantCulture,
                                              "{0}/Me/Calendar/Events?{1}",
                                              _serviceInfo.ApiEndpoint,
                                              String.Join("&", queryParameters));

            int calendarItemCount = (await GetCalendarEventsRequest(requestUrl, true));

            if (calendarItemCount > 1)
            {
                return(null);
            }
            if (calendarItemCount == 1)
            {
                return(await UpdateCalendarEvent(calendarEvent));
            }

            requestUrl = String.Format(CultureInfo.InvariantCulture,
                                       "{0}/Me/Calendar/Events",
                                       _serviceInfo.ApiEndpoint);

            // string postData = JsonConvert.SerializeObject(calendarEvent);
            string postData = CommonCode.Serialize(calendarEvent);

            Func <HttpRequestMessage> requestCreator = () =>
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
                request.Content = new StringContent(postData);
                request.Headers.Add("Accept", "application/json;odata=minimalmetadata");
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return(request);
            };

            HttpResponseMessage response = await Office365Helper.SendRequestAsync(_serviceInfo, _httpClient, requestCreator);

            string responseString = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                await Office365Helper.ShowErrorMessageAsync(_serviceInfo, responseString);

                return(null);
            }

            // CalendarEvent c = JsonConvert.DeserializeObject<CalendarEvent>(responseString);
            CalendarEvent c = CommonCode.Deserialize <CalendarEvent>(responseString);

            return(c);
        }
Exemple #2
0
        public static async Task SendMail(Message mailMessage)
        {
            // Obtain information for communicating with the service:
            Office365ServiceInfo serviceInfo = await Office365ServiceInfo.GetExchangeServiceInfoAsync();

            if (!serviceInfo.HasValidAccessToken)
            {
                await Office365Helper.ShowErrorMessageAsync("Unable to get Exchange ServiceInfo", string.Empty);

                return;
            }

            HttpClient client = new HttpClient();

            // Create a URL for retrieving the data:
            string[] queryParameters = new string[]
            {
                "MessageDisposition=SendOnly"
            };

            string requestUrl = String.Format(CultureInfo.InvariantCulture,
                                              "{0}/Me/Messages?{1}",
                                              serviceInfo.ApiEndpoint,
                                              String.Join("&", queryParameters));

            // string postData = JsonConvert.SerializeObject(mailMessage);
            string postData = CommonCode.Serialize(mailMessage);

            Func <HttpRequestMessage> requestCreator = () =>
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
                request.Content = new StringContent(postData);
                request.Headers.Add("Accept", "application/json;odata=minimalmetadata");
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return(request);
            };

            using (HttpResponseMessage response = await Office365Helper.SendRequestAsync(
                       serviceInfo, client, requestCreator))
            {
                // Read the response and deserialize the data:
                string responseString = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    await Office365Helper.ShowErrorMessageAsync(serviceInfo, responseString);
                }

                // TODO maybe display a confirmation.
            }
        }
        public async Task <CalendarEvent> UpdateCalendarEvent(CalendarEvent calendarEvent)
        {
            string requestUrl = String.Format(CultureInfo.InvariantCulture,
                                              "{0}/Me/Calendar/Events('{1}')",
                                              _serviceInfo.ApiEndpoint,
                                              _calendarItemIdForUpdate);

            // string postData = JsonConvert.SerializeObject(calendarEvent);
            string postData = CommonCode.Serialize(calendarEvent);

            Func <HttpRequestMessage> requestCreator = () =>
            {
                var patch = new HttpMethod("PATCH");
                HttpRequestMessage request = new HttpRequestMessage(patch, requestUrl);
                request.Content = new StringContent(postData);
                request.Headers.Add("Accept", "application/json;odata=minimalmetadata");
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return(request);
            };

            HttpResponseMessage response = await Office365Helper.SendRequestAsync(_serviceInfo, _httpClient, requestCreator);

            string responseString = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                await Office365Helper.ShowErrorMessageAsync(_serviceInfo, responseString);

                return(null);
            }

            // CalendarEvent c = JsonConvert.DeserializeObject<CalendarEvent>(responseString);
            CalendarEvent c = CommonCode.Deserialize <CalendarEvent>(responseString);

            return(c);
        }