示例#1
0
        /// <summary>
        /// Adds a new event to user's default calendar
        /// </summary>
        /// <param name="LocationName">string. The name of the event location</param>
        /// <param name="BodyContent">string. The body of the event.</param>
        /// <param name="Attendees">string. semi-colon delimited list of invitee email addresses</param>
        /// <param name="EventName">string. The subject of the event</param>
        /// <param name="start">DateTimeOffset. The start date of the event</param>
        /// <param name="end">DateTimeOffset. The end date of the event</param>
        /// <returns></returns>
        internal async Task <String> AddCalendarEventAsync(

            string LocationName,
            string BodyContent,
            string Attendees,
            string Subject,
            DateTimeOffset start,
            DateTimeOffset end
            )
        {
            string   newEventId = string.Empty;
            Location location   = new Location();

            location.DisplayName = LocationName;
            ItemBody body = new ItemBody();

            body.Content     = BodyContent;
            body.ContentType = BodyType.Text;
            string[] splitter            = { ";" };
            var      splitAttendeeString = Attendees.Split(splitter, StringSplitOptions.RemoveEmptyEntries);

            Attendee[] attendees = new Attendee[splitAttendeeString.Length];
            for (int i = 0; i < splitAttendeeString.Length; i++)
            {
                attendees[i]              = new Attendee();
                attendees[i].Type         = AttendeeType.Required;
                attendees[i].EmailAddress = new EmailAddress()
                {
                    Address = splitAttendeeString[i].Trim()
                };
            }


            Event newEvent = new Event
            {
                Subject   = Subject,
                Location  = location,
                Attendees = attendees,
                Start     = start,
                End       = end,
                Body      = body,
            };


            newEvent.Start = (DateTimeOffset?)CalcNewTime(newEvent.Start, start);
            newEvent.End   = (DateTimeOffset?)CalcNewTime(newEvent.End, end);

            try
            {
                // Make sure we have a reference to the Outlook Services client
                var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");

                // This results in a call to the service.
                await outlookServicesClient.Me.Events.AddEventAsync(newEvent);

                await((IEventFetcher)newEvent).ExecuteAsync();
                newEventId = newEvent.Id;
            }
            catch (Exception e)
            {
                throw new Exception("We could not create your calendar event: " + e.Message);
            }
            return(newEventId);
        }