Question

Krushna hond on Fri, 15 Apr 2016 12:52:02



I am trying to create event for all day using o365 event rest api but getting error start and end time should be midnight I tried using below start date & End date for all day event e.g. Start Date: 01/01/2016 12:00 AM (dd/MM/yyyy) End Date: 02/01/2016 12:00 AM (dd/MM/yyyy) As api says there should be 24 hours gap for all day event I did the same way still its throwing error.

I tried different case for creating event but there is difference between the dates which I passed to rest api, I tried with passing time zone too but still there is difference.

using API 2.0 getting differnt issue. Incompatible type kinds were found. The type 'Microsoft.OutlookServices.DateTimeTimeZone' was found to be of kind 'Complex' instead of the expected kind 'Primitive'.

var startDt=new DateTime(2016, 1, 22, 00, 00, 0);
startDate.DateTime = startDt.ToString(dateTimeFormat);
startDate.TimeZone = timeZone;

DateTimeTimeZone endDate = new DateTimeTimeZone();
endDate.DateTime = startDt.AddDays(1).ToString(dateTimeFormat);
endDate.TimeZone = timeZone;

Event newEvent = new Event
{
Subject = "Test Event",
Location = location,
Start = startDate,
End = endDate,
Body = body
};

            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;


Sponsored



Replies

Edward Z on Mon, 18 Apr 2016 02:49:41


Hi Krushna,

This forum is used to discuss about Office add ins, your issue is related with Office 365 Api developing, I will move this thread to the more related forum.

Reference: https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=Office365forDevelopers

Thanks for your understanding.

Best Regards,

Edward

Nan Yu on Tue, 19 Apr 2016 01:30:37


You could use DateTimeOffset.Parse function to represent a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC). To create a day event in O365 , you could try below tested code :

DateTimeOffset start = DateTimeOffset.Parse("2016/04/19 00:00");
DateTimeOffset end = DateTimeOffset.Parse("2016/04/20 00:00");

Pass the DateTimeOffset object and create an event :

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() };
            }


            //newEvent.Attachments.Add(ae);
            Event newEvent = new Event
            {
                Subject = Subject,
                Location = location,
                Attendees = attendees,
                Start = start,
                End = end,
                Body = body,
                IsOrganizer = false,
            };


          
 
            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;
        }

                           /// <summary>
        /// Sets new time component of the datetimeoffset from the TimeSpan property of the UI
        /// </summary>
        /// <param name="OldDate">DateTimeOffset. The original date</param>
        /// <param name="NewDate">DateTimeOffset. The new date</param>
        internal DateTimeOffset CalcNewTime(DateTimeOffset? OldDate, DateTimeOffset NewDate)
        {
            //Default return value to New start date
            DateTimeOffset returnValue = NewDate;
            return returnValue;
        }

And the result :