Error EWS The specified time zone isn't valid .net - c#

im using EWS to send appointments to accounts of my corporation, developed on c# .net MVC 3.
Code
var service = new ExchangeService(ExchangeVersion.Exchange2013,TimeZoneInfo.Local);
service.Credentials = new WebCredentials("myuser", "mypassowrd", "mycompany");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.Url = new Uri(uriString: "myexchangeserver" );
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");
Appointment appointment = new Appointment(service);
appointment.Subject = "TestMeeting";
appointment.Body = "The purpose of this meeting is to discuss status.";
appointment.Start = new DateTime(2017, 12, 12, 9, 0, 0);
appointment.End = appointment.Start.AddHours(2);
appointment.Location = "Conf Room";
appointment.RequiredAttendees.Add("myemail#mycompany.cl");
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
and my problem is the following, when I execute the code until the final line, the next exception returns:
{"The specified time zone isn't valid."}
and I have not been able to find any solution, if you need more information, indicate in comments, thank you very much and sorry for my bad english :c

This error has been reported as an issue in the EWS API on github.
However, it looks to be more likely an issue in .NET's TimeZoneInfo parsing logic. Updating your .net version might be the solution.
Here is an article containing other possible solutions.

Related

Can't limit access to shared calendar through impersonation using EWS

We're using EWS to integrate our CRM with Exchange Online 2010SP2. One of the tasks: provide one calendar for Sales persons with the next rule: every Sales can see all appointments in Scheduler, but can open and see details (body) only his/her own appointment. Appointment are being placed by CRM in response of certain business events. We tried to use impersonate and Sensitivity.Private property. Appointment is being placed with impersonated user name, but it can't be opened by that user neither through OWA nor via Outlook. So it's placed as private appointment of 'master' user (user who created shared calendar and his credentials is used for service connection), he can open it in Outlook or OWA. EWSEditor shows appointment's LastModifiedUser - correct impersonated username (not master's). In Fiddler we can see 'success' Response on appointment placement request (under impersonated user). In OutlookSpy, we can see that appointments PR_SENDER_NAME_W, PR_SENT_REPRESENTING_NAME_W properties shows 'master's' username. We stuck.
Impersonated user has 'owner' rights upon that shared calendar.
If Impersonating doesn't resolve this issue, can Delegate technique do that?
Thanks in advance for any help.
static void Main(string[] args)
{
ExchangeService services = new
ExchangeService(ExchangeVersion.Exchange2010_SP2);
services.Credentials = new WebCredentials("master#exchserver.com",
"MasterPwd");
services.Url = new Uri("https://someserver.com/ews/exchange.asmx");
FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Calendar);
FolderView fvFolderView = new FolderView(100);
DateTime startDate = DateTime.Now.AddDays(1);
DateTime endDate;
string SalesCalendarId = "AAMkADVlMGVjZWVkLT....AADo8XAAA=";
CalendarFolder folder = CalendarFolder.Bind(services, new
FolderId(SalesCalendarId));
TimeSpan ts = new TimeSpan(10, 00, 0);
startDate = startDate.Date + ts;
endDate = startDate.AddMinutes(60);
services.HttpHeaders.Add("X-AnchorMailbox","impersonateduser#exchserver.com");
appointment.Subject = "from Test";
appointment.Body = "Test";
appointment.Start = startDate;
appointment.End = appointment.Start.AddMinutes(30);
appointment.ReminderDueBy = appointment.Start.AddHours(1);
appointment.Sensitivity = Sensitivity.Private;
ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "impersonateduser#exchserver.com");
appointment.Save(SalesCalendarId, SendInvitationsMode.SendToNone);
}
You could use impersonated user to obtain ‘owner’ permissions.
Please include the code below before services.Credentials = new WebCredentials("master#exchserver.com", "MasterPwd")
services.PreAuthenticate = true;
you need to make sure that we have required permissions for EWS Impersonation as per the article mentioned below:
Configuring Exchange Impersonation in Exchange 2010
Using Exchange Impersonation in Exchange 2010
You could reference the following link:
Get all calendar events for a specific user

Creating appointment on Exchange server calendar as other user without impersonation (EWS)

I am creating simple app for appointments scheduling and I want to implement ability for me to create appointments for my users.
I managed to create,update and delete my calendar on Exchange Server, and I somewhat managed to create appointments adding my colleagues as RequiredAttendees like so:
//service variable is being created using my credidentals
Appointment meeting = new Appointment(service);
meeting.Subject = "Some subject ";
meeting.Body = "Some body.";
meeting.Start = DateTime.Now;
meeting.End = meeting.Start.AddHours(4);
meeting.Location = "Some Location";
meeting.RequiredAttendees.Add("myCollegue#mail.com");
meeting.ReminderMinutesBeforeStart = 60;
meeting.Save(new FolderId(WellKnownFolderName.Calendar,
"myCollegue#mail.com"),
SendInvitationsMode.SendToAllAndSaveCopy);
But it is just setting him as required attendee. Next thing is I tried using impersonation, but I can't access hosting server to set myself as master and others to have to share calendar with me (due to permissions and stuff) so I had to scrape that as well. Also, he set me up to be his publishing author on his calendar.
Is there something I am missing, or can't seem to find on MSDN sites?
EDIT: I am able to create appointment in his calendar in outlok.
If anyone comes across same issues as I did in here please follow these steps:
Make sure that person for which you are creating appointment sets you up (on exchange server or in outlok as "Editing author" with all permissions.
After that you can create appointments for him (verify this by going to your outlok and creating some test appointments).
This code works for me:
Folder inboxFolder = Folder.Bind(service, new FolderId(WellKnownFolderName.Calendar, "your.colleague#company.com"));
Appointment appointmentOther = new Appointment(service);
appointmentOther.Subject = "Test 2";
appointmentOther.Body = "Body text";
appointmentOther.Start = DateTime.Now;
appointmentOther.End = DateTime.Today.AddHours(16);
appointmentOther.Location = "My Office";
appointmentOther.IsReminderSet = true;
appointmentOther.ReminderMinutesBeforeStart = 30;
appointmentOther.Save(inboxFolder.Id,SendInvitationsMode.SendToNone);
Good luck :)

iCal Time Zone issue

I'm trying to allow a user to download an iCal for their calendar in ASP.Net, but am having a timezone issue.
If I download the file on my computer, the time appears correct and within the correct timeframe. However, when I try to download it on a phone, the timezone switches and it becomes 5 hours behind (aka 7:00 AM becomes 3:00 AM).
Does anyone know how to fix this issue/set the timezone?
Here is the code:
iCalendar iCal = new iCalendar();
Event evt = iCal.Create<Event>();
DateTime dt = (DateTime)Convert.ToDateTime(lblTicketDue.Text);
Console.Write(dt);
evt.Start = new iCalDateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
evt.End = new iCalDateTime((DateTime)Convert.ToDateTime(lblTicketDue.Text).AddMinutes(15.0));
Alarm alarm = new Alarm();
alarm.Action = AlarmAction.Display;
alarm.Summary = "Ticket due!";
Trigger t = new Trigger();
iCalDateTime icdt = new iCalDateTime(dt.Subtract(TimeSpan.FromMinutes(120.0)));
t.DateTime = icdt;
alarm.Trigger = t;
evt.Alarms.Add(alarm);
iCal.Events.Add(evt);
iCalendarSerializer serializer = new iCalendarSerializer();
string output = serializer.SerializeToString(iCal);
Response.ContentType = "text/calendar";
Response.Write(output);
Response.End();
Hard to tell without looking at the actual iCalendar stream that gets generated but it is quite likely that you are generating your DTSTART/DTEND using floating time (e.g. "20160517T070000" ).
If the event is not recurring (no RRULE), what you want to do is convert your datetime to UTC and use the "date with UTC time" format described in https://www.rfc-editor.org/rfc/rfc5545#section-3.3.5
i.e. something like "20160517Txx0000Z"
If the event is recurring you would then need to use the last form (date with local time and timezone reference).

iCal client support for multiple event requests

When a user signs up on our website, I'd like to send them an email that allows them to automatically update their calendar with the classes they have enrolled in. In most cases this will be multiple days/events.
As a test I'm using DDay.ical to create a multi-event request. However, it doesn't seem like either Outlook or the iPhone mail app notices the second event in the ical attachment.
I know that multiple events are supported in the iCal standard. How that doesn't mean that all clients support that scenarios. Do other clients support multi-event ical requests?
I don't think I'm doing anything wrong in code, but I'll post my code fragment to be sure:
// Create event part.
iCalendar iCal1 = new iCalendar();
iCal1.AddLocalTimeZone();
iCal1.Method = "REQUEST";
Event evt1 = iCal1.Create<Event>();
evt1.Start = new iCalDateTime(new DateTime(2014, 8, 4, 12, 30, 00, DateTimeKind.Local));
evt1.End = evt1.Start.AddMinutes(30);
evt1.IsAllDay = false;
evt1.Summary = string.Format("Lesson - {0}", evt1.Start.ToString("MM/dd"));
evt1.Location = "Anytown";
// Add recipients for appointment.
Attendee att1 = new Attendee("mailto:" + "me#MyDomain.com");
att1.RSVP = false;
att1.CommonName = "Me Jones";
evt1.Attendees.Add(att1);
Event evt2 = iCal1.Create<Event>();
evt2.Start = new iCalDateTime(new DateTime(2014, 8, 11, 12, 30, 00, DateTimeKind.Local));
evt2.End = evt1.Start.AddMinutes(30);
evt2.IsAllDay = false;
evt2.Summary = string.Format("Lesson - {0}", evt2.Start.ToString("MM/dd"));
evt2.Location = "AnyTown";
// Add recipients for appointment.
Attendee att2 = new Attendee("mailto:" + "me#MyDomain.com");
att2.RSVP = false;
att2.CommonName = "Me Jones";
evt2.Attendees.Add(att2);
iCalendarSerializer serializer1 = new iCalendarSerializer();
string t = serializer1.SerializeToString(iCal1);
Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(t);
using (var ms = new System.IO.MemoryStream(bytes))
{
using (var a = new System.Net.Mail.Attachment(ms, "meeting.ics", "text/calendar")) //Either load from disk or use a MemoryStream bound to the bytes of a String
{
a.ContentDisposition.Inline = true; //Mark as inline
msg.Attachments.Add(a); //Add it to the message
Mailer.Send(msg);
}
}
Unfortunately, you're completely dependent on the implementation of Icalendar in the various email clients, and these are generally very protective of their users' calendars. They generally all support multi-event Icalendars, but invitations that "go straight in" to a users calendar have to be sent one event at a time. I'm not aware of any exceptions to this.
To process an Icalendar attachment containing more than one event, in Outlook for example, you need to save the attachment to disk, navigate to it and open it. It then opens as a separate calendar, and you need to drag the events one by one into your calendar. Nightmare. This will rarely be worth the trouble to develop.
Another option of course is to host the Icalendar on your website and get your users to subscribe by entering the calendar URL in their client. This has the advantage that changes propagate automatically, but email clients will still treat the events as external (no automatic reminders, in outlook the default is to display them in a separate pane, Gmail at least displays events from different calendars on the same grid.)

RemoveExtendedProperty throws error when used on an occurrence of a recurring appointment

I am developing an application that syncs an exchange calendar to another calendar. I put extended properties on the exchange appointments in order to preserve the mapping between appointments in the two calendars. Everything is working fine until I try to remove an extended property from an occurrence of a recurring appointment. When I try doing this, I get the error:
The delete action is not supported for this property.
Here is a code snippet that demonstrates the error:
public void ExchangeTest()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
{
Credentials = new NetworkCredential("username", "password", "domain")
};
service.AutodiscoverUrl("username#domain.com");
Appointment appt = new Appointment(service)
{
Recurrence = new Recurrence.DailyPattern(DateTime.Now, 2) { NumberOfOccurrences = 3},
Start = DateTime.Now,
End = DateTime.Now.AddHours(2),
Subject = "Test Appointment"
};
NameResolutionCollection resolutionCollection = service.ResolveName("username", ResolveNameSearchLocation.DirectoryOnly, false);
string mailboxAddress = resolutionCollection.First().Mailbox.Address;
FolderId folderId = new FolderId(WellKnownFolderName.Calendar, mailboxAddress);
appt.Save(folderId);
PropertySet properties = new PropertySet(AppointmentSchema.ICalUid);
appt.Load(properties);
CalendarView view = new CalendarView(DateTime.Today, DateTime.Today.AddDays(8)){PropertySet = properties};
IEnumerable<Appointment> occurrences = service.FindAppointments(folderId, view)
.Where(a => a.ICalUid == appt.ICalUid);
ExtendedPropertyDefinition definition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "TestProperty", MapiPropertyType.String);
Appointment firstOccurrence = occurrences.First();
firstOccurrence.SetExtendedProperty(definition, "test");
firstOccurrence.Update(ConflictResolutionMode.AutoResolve);
//The error occurs on the next line.
firstOccurrence.RemoveExtendedProperty(definition);
firstOccurrence.Update(ConflictResolutionMode.AutoResolve);
//clean up
appt.Delete(DeleteMode.HardDelete);
}
It appears that the error is only thrown for an Exchange 2007 server (It works on 2010). Am I doing something wrong, or is this a problem with Exchange? Is there a way to work around this issue? Any help will be appreciated.
I ended up not using the RemoveExtendedProperty function. Instead, I worked around it by just setting the property again, but setting it to an empty space. I then handled the empty space in code. This appears to be a problem with Exchange or the managed API.
Did you try;
appointment.Delete(DeleteMode.SoftDelete,SendCancellationsMode.SendToAllAndSaveCopy);

Categories