none
How to get Work time, Timezone from Calendar using EWS API? RRS feed

  • Question

  • Hi,

    In the Outlook options we can set Work Time & Time Zone for Calendar. I want to read following using EWS api 2.0 in C# for a particular user.

    1) Start time & End time for Work hours.

    2) TimeZone set for a particular user in the calendar.

    3) Also any other item set as a part of outlook options.

    Please guide how to read these configuration settings using EWS API.

    Thanks

    Varun


    Varun Yadav

    Friday, May 3, 2013 4:20 PM

All replies

  • You can use the UserConfiguration operation in EWS to do that eg http://msdn.microsoft.com/en-us/library/exchange/gg274398(v=exchg.80).aspx

    For the Start and End Work hours and TimeZone you can use

                UserConfiguration WorkHours;
                WorkHours = UserConfiguration.Bind(service, "WorkHours", WellKnownFolderName.Calendar, UserConfigurationProperties.All);
                XmlDocument xmlConfig = new XmlDocument();
                xmlConfig.LoadXml(Encoding.UTF8.GetString(WorkHours.XmlData));
    
                XmlNodeList xmlList = xmlConfig.GetElementsByTagName("TimeSlot");
                if (xmlList.Count > 0) {
                    for (Int32 ncCnt = 0; xmlList[0].ChildNodes.Count > ncCnt;ncCnt++ )
                    {
                        XmlNode ChildNode = xmlList[0].ChildNodes[ncCnt];
                        if (ChildNode.Name == "Start")
                        {
                            Console.WriteLine(ChildNode.InnerText);
                        }
                        if (ChildNode.Name == "End")
                        {
                            Console.WriteLine(ChildNode.InnerText);
                        }
                    }
                    
                }
                XmlNodeList xmlList1 = xmlConfig.GetElementsByTagName("TimeZone");
                if (xmlList1.Count > 0)
                {
                    for (Int32 ncCnt = 0; xmlList1[0].ChildNodes.Count > ncCnt; ncCnt++)
                    {
                        XmlNode ChildNode = xmlList1[0].ChildNodes[ncCnt];
                        if (ChildNode.Name == "Name") {
                            Console.WriteLine(ChildNode.InnerText);
                        }
                    }
    
                }
     

    The format of the XMLdocument is documented in http://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx

    Cheers
    Glen

    Monday, May 6, 2013 7:48 AM
  • Thanks Glen for the reply. Actually I am facing an issue EWS API 2.0. Please look at the function below in which I want to get Suggested meeting slot of user1 in the PST Time Zone & running that code in the IST time zone user.

    Issue:

    Actually working hours of user1 which is 8 AM To 5 PM (PST) is spread in IST zone like (12 AM to 5 AM (IST) ) and (8:30 PM to 11:30 PM in IST). But when I look suggested time the code gives only suggested time between time slot  (12 AM to 5 AM (IST) ) & doesn't give any suggested time slot between (8:30 PM to 11:30 PM in IST). Although I have set  myOptions.MaximumNonWorkHoursSuggestionsPerDay =0; so why it is skipping  (8:30 PM to 11:30 PM in IST) which are also working hours user1 which is in PST time zone. Please can you advise why EWS API is giving results like that.

    Note: It is gives suggested time slot between  (8:30 PM to 11:30 PM in IST) when I look for suggested time slots of same day only. But when I expand suggestion windows for than one day it only gives suggested time slots between (12 AM to 5 AM (IST) ) & not giving any suggested time slot in between  (8:30 PM to 11:30 PM in IST). Please can you advise me how to get all slots in any day as per the working hours.

     private static void GetSuggestedMeetingTimes(ExchangeService service)
            {
                // Create a list of attendees.
                List<AttendeeInfo> attendees = new List<AttendeeInfo>();

                attendees.Add(new AttendeeInfo()
                {
                    //User in PST TIME Zone & It's working Hours are 8 AM to 5 PM in PST
                    SmtpAddress = "user1@contsos.com",
                    AttendeeType = MeetingAttendeeType.Required
                });
                        

                // Specify suggested meeting time options.
                AvailabilityOptions myOptions = new AvailabilityOptions();
                myOptions.MeetingDuration = 30;
                myOptions.MaximumNonWorkHoursSuggestionsPerDay =0;
                myOptions.GoodSuggestionThreshold = 49;
                myOptions.MinimumSuggestionQuality = SuggestionQuality.Excellent;
                myOptions.DetailedSuggestionsWindow = new TimeWindow(DateTime.Now, DateTime.Now.AddDays(2));


              
                // Return a set of suggested meeting times.
                GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
                                                                             new TimeWindow(DateTime.Now, DateTime.Now.AddDays(5)),
                                                                                 AvailabilityData.FreeBusyAndSuggestions,
                                                                                 myOptions);
                // Display available meeting times.
              
                Console.WriteLine("Availability for {0}", attendees[0].SmtpAddress);
                Console.WriteLine();

                foreach (AttendeeAvailability aa in results.AttendeesAvailability)
                {
                    Console.WriteLine("Start Hours : {0}",aa.WorkingHours.StartTime.ToString());
                    Console.WriteLine("End Hours : {0}",aa.WorkingHours.EndTime.ToString());
                    Console.WriteLine("Time Zone : {0}",aa.WorkingHours.TimeZone.ToString());               

                }

                foreach (Suggestion suggestion in results.Suggestions)
                {
                    Console.WriteLine(suggestion.Date);
                    Console.WriteLine();
                    foreach (TimeSuggestion timeSuggestion in suggestion.TimeSuggestions)
                    {
                        Console.WriteLine("Suggested meeting time:" + timeSuggestion.MeetingTime);
                        Console.WriteLine();
                    }
                }
                foreach (AttendeeAvailability availability in results.AttendeesAvailability)
                {
                    Console.WriteLine(availability.Result);
                    Console.WriteLine();
                    foreach (CalendarEvent calendarItem in availability.CalendarEvents)
                    {
                        Console.WriteLine("Free/busy status: " + calendarItem.FreeBusyStatus);
                        Console.WriteLine("Start time: " + calendarItem.StartTime);
                        Console.WriteLine("End time: " + calendarItem.EndTime);
                        Console.WriteLine();
                    }
                }
            }


    Varun Yadav


    Wednesday, May 8, 2013 2:47 PM
  • First question and something to test is does this work in Outlook ? Eg in the Suggested Meeting windows when you add the appointment and options you want, What do do you see in the suggested times box.

    If you see it working the way you want, I would suggest you then install fiddler http://fiddler2.com/ on machine your using Outlook on. You can capture the EWS request that outlook is making and have a look at the XML to work out what the correct settings are for what result you want to get.

    Cheers
    Glen

    Thursday, May 9, 2013 6:55 AM