private static async Task <SampleModel.Exchange.CalendarEvent[]> GetCalendarEvents()
        {
            // Obtain information for communicating with the service:
            Office365ServiceInfo serviceInfo = await Office365ServiceInfo.GetExchangeServiceInfoAsync();

            if (!serviceInfo.HasValidAccessToken)
            {
                return(null);
            }

            // Create a URL for retrieving the data:
            string[] queryParameters =
            {
                String.Format(CultureInfo.InvariantCulture, "$filter=End ge {0}Z", DateTime.UtcNow.ToString("s")),
                "$top=10",
                "$select=Subject,Start,End"
            };
            string requestUrl = String.Format(CultureInfo.InvariantCulture,
                                              "{0}/Me/Calendar/Events?{1}",
                                              serviceInfo.ApiEndpoint,
                                              String.Join("&", queryParameters));

            // Prepare the HTTP request:
            using (HttpClient client = new HttpClient())
            {
                Func <HttpRequestMessage> requestCreator = () =>
                {
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
                    request.Headers.Add("Accept", "application/json;odata=minimalmetadata");
                    return(request);
                };

                // Send the request using a helper method, which will add an authorization header to the request,
                // and automatically retry with a new token if the existing one has expired.
                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);

                        return(null);
                    }

                    var events = JObject.Parse(responseString)["value"].ToObject <SampleModel.Exchange.CalendarEvent[]>();
                    events = events.OrderBy(e => e.Start).ToArray();
                    return(events);
                }
            }
        }
Esempio n. 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 static async Task <CalendarEvents> CreateCalendarInstance()
        {
            CalendarEvents calendarEvents = new CalendarEvents();

            // Obtain information for communicating with the service:
            calendarEvents._serviceInfo = await Office365ServiceInfo.GetExchangeServiceInfoAsync();

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

                return(null);
            }

            calendarEvents._httpClient = new HttpClient();

            return(calendarEvents);
        }