简体   繁体   中英

How to return EWS calendar appointment as JSON?

I'm trying to get calendar events using EWS Managed API. I used the example provided by Microsoft on their website.

 public class CalendarController : ApiController
        {
            public static ExchangeService service;
            // GET api/calendar
            public IEnumerable<string> Get()
            {
                // Initialize values for the start and end times, and the number of appointments to retrieve.
                DateTime startDate = DateTime.Now;
                DateTime endDate = startDate.AddDays(30);
                const int NUM_APPTS = 50;

                service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
                {
                    Credentials = new NetworkCredential("******@example.com", "******"),
                    Url = new Uri("url to ews")
                };

                // Initialize the calendar folder object with only the folder ID. 
                CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

                // Set the start and end time and number of appointments to retrieve.
                CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);

                // Limit the properties returned to the appointment's subject, start time, and end time.
                cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

                System.Diagnostics.Debug.WriteLine("getting appointments now");
                // Retrieve a collection of appointments by using the calendar view.
                FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
                System.Diagnostics.Debug.WriteLine(appointments.TotalCount);
                System.Diagnostics.Debug.WriteLine(appointments.ToArray());

                List<string> appts = new List<string>();

                foreach (Appointment a in appointments)
                {
                    appts.Add(a.Id.ToString());
                    appts.Add(a.Subject);
                    appts.Add(a.Start.ToString());
                    appts.Add(a.End.ToString());            
                }

                //var appts = appointments.ToList();
                return appts;
                //return Json(appts, JsonRequestBehavior.AllowGet);
            }

I'm using WebAPI to Get results for FullCalendar. However, it doesn't return the data in json in key value form. I also tried using Dictionary, but that also generated error about having the name of the object already existing; I added apts.Add("ID", a.Id.tostring()) . How can I make it to return in JSON format with the object name and value?

OK, just to be clear this isn't really about EWS or the calendar appointment, it's about serializing objects coming back from WebApi.

The reason you are not seeing property names returned in the JSON is because you didn't return an object with properties, you returned a List<string> or if you prefer, an array of strings. As you said in your comment, the return values look like:

["AAM","A subject","1/5/2015 12:00:00 AM","1/6/2015 12:00:00 AM" .. ]

If you want to return an object with properties, you can either return the Appointment object instead of List<string> , or if you need to return more than one object you can change it to IEnumerable<Appointment> . If you don't want to return the Appointment object because it contains extra info, then create your own class and return that object instead.

I modified your code to be more generic, but it would look something like this:

public IEnumerable<Appointment> Get()
{
    // Initialize values for the start and end times, and the number of appointments to retrieve.
    DateTime startDate = DateTime.Now;
    DateTime endDate = startDate.AddDays(30);
    const int NUM_APPTS = 50;

    // Snipped for brevity

    // Retrieve a collection of appointments by using the calendar view.
    FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

    return  appointments.AsEnumerable();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM