7

I'm trying to use the Exchange 2007 API to query calendar availability for a specific user. My sample code is producing the following exception:

The time duration specified for FreeBusyViewOptions.TimeWindow is invalid.

Here's the sample code:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

service.AutodiscoverUrl("email@domain.com");

DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0);

TimeWindow tw = new TimeWindow(startTime, startTime.AddHours(8));

GetUserAvailabilityResults result = service.GetUserAvailability(new List<AttendeeInfo> { new AttendeeInfo("email@domain.com") }, tw, AvailabilityData.FreeBusyAndSuggestions);

The wierd thing is, if I replace my startTime assignment with the following it works:

DateTime startTime = DateTime.Now;

What's the difference between the DateTime object I created and the object produced by DateTime.Now. I've examined them in detail while debugging and can't find a difference.

Any ideas?

joshb
  • 5,182
  • 4
  • 41
  • 55
  • date time now gives you a current datetime, and datetime you provided is 1/6/2012 7:00:00 am – COLD TOLD Jan 05 '12 at 22:46
  • I realize the actual date/time is different but even if I set my object's date/time equal to Now it has the same issue. – joshb Jan 05 '12 at 22:53

6 Answers6

26

This actually appears to be an issue in the GetUserAvailability method as opposed to any DateTime manipulation.

According to the MSDN documentation:

The GetUserAvailability(Generic ,TimeWindow,AvailabilityData,AvailabilityOptions) method supports only time periods that are a minimum of 24 hours long and that begin and end at 12:00a.m. To restrict the results of the method to a shorter time period, you must filter the results on the client.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Thank you. I looked through the documentation several times and totally missed that. Weird thing is that that's not always true (sometimes it works with non-12am starts and < 24 hour durations) according to my testing but if I stay within those parameters it always seems to work. – joshb Jan 06 '12 at 14:58
  • I had the same Exception. The problem was that my query, that also contains a start and end date "DefineAvailabilityOptions(query)", did not match with the provided start and end date in GetUserAvailability. So before drive crazy with the 24h limit you may want to check this first at least. – Marc Juchli Nov 04 '14 at 14:00
2

I find out that the specified TimeWindow must contain at least one midnight. But I do not know why.

2

Maybe it has something to do with the difference between your time zone and UTC, producing a negative time window. Try increasing from AddHours(8) to bigger values up to AddHours(24) and see what happens.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • Your suggestion eliminated the exception (thanks) but I'm not really sure why though. Either way, I'm adding hours to the same DateTime so how could I end up with a negative time window whether it's local or UTC? – joshb Jan 05 '12 at 23:02
2

Specify the Kind to make it identical to Now:

     DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0, DateTimeKind.Local);

With some odds that you actually need Utc. Depends on server config probably.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

The Kind is different. This may be what it is looking for.

new DateTime(2012, 1, 6, 7, 0, 0)

has a kind of "Unspecified".

While

DateTime.Now

has a Kind of "Local".

Try using ToLocalTime to set the kind to local:

DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0).ToLocalTime();
Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • I noticed that too when I was debugging but using the ToLocalTime method or passing the DateTimeKind into the constructor didn't change anything. – joshb Jan 05 '12 at 22:56
0

Look at the constructors and code for the class DateTime.

All of them alter the private variable:

private ulong dateData;

So all the constructors are the same and DateTime.Now is a public static method that returns an instance of the DateTime class that does the same thing.

The error message stated:

The time duration specified for FreeBusyViewOptions.TimeWindow is invalid.

That is because it is invalid!

You put in a future date, and it most likely checked for that. Try with a current date.

Issa Fram
  • 2,556
  • 7
  • 33
  • 63
  • looking at the DateTime.Now method, all of its returns are of the format... return new DateTime(value, DateTimeKind.Local... – Issa Fram Jan 05 '12 at 22:56
  • I've tried the current date and it produces the same exception. The whole point of the GetUserAvailability method is to check future calendar availability so it wouldn't make much sense if it didn't accept future date windows. – joshb Jan 05 '12 at 23:09