I am currently developing an app which checks one or more users calendars for appointments/meetings under a specific category.
Being new to working with EWS, i have been trying to find a solution as to get a Calendar item (appointment or meeting) by Category or determine if an appointment has a specific category. I currently have the following code so far (exService = ExchangeService object):
foreach (Appointment a in exService.FindItems(WellKnownFolderName.Calendar, new ItemView(int.MaxValue)))
{
//Need to check if appointment has category f.x.: "SMS"
}
Does anybody know a way to acheive this?
Thanks
When your querying Appointments you want to use FindAppointments and a calender-view rather then using FindItems this will ensure that any recurring appointments are expanded eg see https://msdn.microsoft.com/en-us/library/office/dn495614(v=exchg.150).aspx
to use categories all you need to do is something like
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(60);
const int NUM_APPTS = 1000;
// 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,AppointmentSchema.Categories);
// Retrieve a collection of appointments by using the calendar view.
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
" to " + endDate.Date.ToShortDateString() + " are: \n");
foreach (Appointment a in appointments)
{
if (a.Categories.Contains("Green"))
{
Console.Write("Subject: " + a.Subject.ToString() + " ");
Console.Write("Start: " + a.Start.ToString() + " ");
Console.Write("End: " + a.End.ToString());
}
Console.WriteLine();
}
Related
I have created a calender.ics file attachment to an email being sent to a user. The user then can add the appointment details to their calendar(whichever they choose Outlook, Google calendar, Yahoo! ect.). The .ics file works beautifully if I click on the attachment and download the file, but I've endlessly searched for a decent example where there is a "Add to Calendar" hyperlink or how to go about it with no real success. Is there way to accomplish this in C#? Am I missing something? This is my first stab at Icalender and I'm using StringBuilder() to build out the .ICS file.
var customerMailMessage = new MailMessage(Store.LocationEmail, CustomerEmail);
customerMailMessage.Subject = "Appointment Request For " + ServiceDate.ToShortDateString();
customerMailMessage.Body = body.ToString();
customerMailMessage.IsBodyHtml = true;
//Start of calender invite
DateTime ServiceTime = ServiceDate.Date.Add(StartTime.TimeOfDay);
DateTime DateStart = Convert.ToDateTime(ServiceTime);
string DateFormat = "yyyyMMddTHHmmssZ";
string now = DateTime.Now.ToUniversalTime().ToString(DateFormat);
string Location = string.Format(Store.LocationAddress1 + " " + Store.LocationCity + " " + Store.LocationState + " " + Store.LocationZip);
string Summary = "Calendar reminder";
string Description = "Your appointment details on: " + DateStart;
string FileName = "ServiceAppointment.ics";
Guid guid = Guid.NewGuid();
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("PRODID:Domain.com");
sb.AppendLine("CALSCALE:GREGORIAN");
sb.AppendLine("METHOD:REQUEST");
sb.AppendLine("BEGIN:VEVENT");
sb.AppendLine("UID:" + guid);
sb.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", retailStore.LocationEmail));
sb.AppendLine(string.Format("ATTENDEE;CN='{0}, {1}';ROLE=REQ-PARTICIPANT;PARTSTAT=TENTATIVE:MAILTO:{2}", CustomerFirstName, CustomerLastName, CustomerEmail));
sb.AppendLine("DTSTART:" + DateStart.ToUniversalTime().ToString(DateFormat));
sb.AppendLine("DTSTAMP:" + now);
sb.AppendLine("SUMMARY: " + Summary);
sb.AppendLine("LOCATION: " + Location);
sb.AppendLine("DESCRIPTION:" + Description);
sb.AppendLine("PRIORITY:5");
sb.AppendLine("TRANSP:OPAQUE");
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
var calendarBytes = Encoding.UTF8.GetBytes(sb.ToString());
System.IO.MemoryStream stream = new System.IO.MemoryStream(calendarBytes);
Attachment attachment = new Attachment(stream, FileName, "text/calendar");
customerMailMessage.Attachments.Add(attachment);
System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "ServiceAppointment.ics");
var emailProvider = new EmailProvider();
emailProvider.Send(customerMailMessage, "Request Appointment - Customer - " + CreatedBySource);
Short-answer: A link in the body of the message would not invoke the method[s] on the server-side to convert the ICS file into a Calendar Item for the user's mailbox; especially, considering each platform mentioned (Exchange, Google Mail, Yahoo Mail, etc.) has it's own implementation for doing so (whether via web or client).
As an example, you can read Microsoft's Open Specification for converting an iCal to an Appointment item here.
I am building an application that accesses meetings from Exchange. I'm using the code provided by Microsoft in their EWS documentation. The issue is I need to access a specific calendar. Say, I create 2 calendars apart from the default one. When I access meetings using this code, I only get meetings from the default calendar. I want to access meetings from a specific calendar. How can I do this?
Thanks for the help.
// 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 = 5;
// 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);
// Retrieve a collection of appointments by using the calendar view.
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
" to " + endDate.Date.ToShortDateString() + " are: \n");
foreach (Appointment a in appointments)
{
Console.Write("Subject: " + a.Subject.ToString() + " ");
Console.Write("Start: " + a.Start.ToString() + " ");
Console.Write("End: " + a.End.ToString());
Console.WriteLine();
}
I think your problem is that you are using WellKnownFolderName.Calendar:
CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
Instead you should use the FolderId of the calendar you have created. To get the id of the folder (calendar) you can use the code similar to this (found in answer: https://stackoverflow.com/a/24133821/1037864)
ExtendedPropertyDefinition PR_Folder_Path = new ExtendedPropertyDefinition(26293, MapiPropertyType.String);
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
psPropSet.Add(PR_Folder_Path);
FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root, mbMailboxname);
FolderView fvFolderView = new FolderView(1000);
fvFolderView.Traversal = FolderTraversal.Deep;
fvFolderView.PropertySet = psPropSet;
SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF.Appointment");
FindFoldersResults ffoldres = service.FindFolders(rfRootFolderid, sfSearchFilter, fvFolderView);
if (ffoldres.Folders.Count > 0) {
foreach (Folder fld in ffoldres.Folders) {
Console.WriteLine(fld.Id.ToString() + " " + fld.DisplayName);
}
}
Take the value from fld.Id and use instead of WellKnownFolderName.Calendar.
(I don't have an Exchange server available right now to try this out but I hope you get the idea and that my code i correct.)
The Problem
I have a Visual Studios 2015 Console Application with the Microsoft.Exchange.WebServices v2.2.0 NuGet package installed. I'm trying to create an appointment, update it, and cancel it while retaining the correct Timezone in the "When" string generated automatically in the calendar invite body. Currently, the initial creation has the correct timezone, but any subsequent updates cause the timezone to revert to UTC.
Note: We have an Exchange 2010 server and use Outlook 2013 clients.
Code Sample
using System;
using System.Globalization;
using Microsoft.Exchange.WebServices.Data;
namespace EWSTesting
{
class Program
{
private const string EmailServer = ""; //replace with your Exchange server
private const string EmailAddress = ""; //replace with your email
static void Main(string[] args)
{
Console.WriteLine("Current Timezone: " + TimeZoneInfo.Local.DisplayName);
var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010, TimeZoneInfo.Local)
{
PreferredCulture = new CultureInfo("en-US"),
Url = new Uri(EmailServer),
UseDefaultCredentials = true
};
Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
var startDate = DateTime.Today;
var endDate = startDate.AddHours(1);
//Create initial appointment
var appointment = new Appointment(exchangeService)
{
Subject = "Testing Appointments",
Body = "Testing Appointments Body",
Location = "Test Location",
LegacyFreeBusyStatus = LegacyFreeBusyStatus.Busy,
Sensitivity = Sensitivity.Private,
Start = startDate,
End = endDate
};
appointment.OptionalAttendees.Add(EmailAddress);
appointment.Save(SendInvitationsMode.SendOnlyToAll);
Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
var appointmentId = appointment.Id;
Console.WriteLine("Pause to check inbox 'When' value on invite");
Console.ReadLine();
appointment = Appointment.Bind(exchangeService, appointmentId);
appointment.Load(new PropertySet(PropertySet.FirstClassProperties)
{
AppointmentSchema.StartTimeZone,
AppointmentSchema.EndTimeZone,
AppointmentSchema.TimeZone
});
appointment.Body = "Body Updated Successfully";
appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);
Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
Console.WriteLine("appointment.StartTimeZone.DisplayName: " + appointment.StartTimeZone.DisplayName);
Console.WriteLine("appointment.EndTimeZone.DisplayName: " + appointment.EndTimeZone.DisplayName);
Console.WriteLine("appointment.TimeZone: " + appointment.TimeZone);
Console.WriteLine();
Console.WriteLine("Pause to check updated inbox 'When' value on invite");
Console.ReadLine();
appointment = Appointment.Bind(exchangeService, appointmentId);
appointment.Load(new PropertySet(PropertySet.FirstClassProperties)
{
AppointmentSchema.StartTimeZone,
AppointmentSchema.EndTimeZone,
AppointmentSchema.TimeZone
});
Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
Console.WriteLine("appointment.StartTimeZone.DisplayName: " + appointment.StartTimeZone.DisplayName);
Console.WriteLine("appointment.EndTimeZone.DisplayName: " + appointment.EndTimeZone.DisplayName);
Console.WriteLine("appointment.TimeZone: " + appointment.TimeZone);
appointment.CancelMeeting();
Console.WriteLine("Appointment Deleted");
Console.ReadLine();
}
}
}
The Results of the above code
Initial Invite (Correct Timezone)
Updated Appointment (Incorrect Timezone in body)
Appointment Cancellation (Incorrect Timezone in body)
Console Result of code provided
What I'm Looking For
I do not need this additional "When" (underlined in red in pictures above) to be appended to the body of the invite. Either I would like to completely remove it (preferred) or I would like to correct it in any updates.
It appears that the issue is a bug in the EWS 2.2.0 DLL, the timezone SOAP headers are not being added to the Update() and CancelMeeting() Exchange transactions. The code below resolves this issue by manually appending the correct header.
For Update():
exchangeService.OnSerializeCustomSoapHeaders += service_OnSerializeCustomSoapHeaders;
appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);
exchangeService.OnSerializeCustomSoapHeaders -= service_OnSerializeCustomSoapHeaders;
For CancelMeeting():
exchangeService.OnSerializeCustomSoapHeaders += service_OnSerializeCustomSoapHeaders;
appointment.CancelMeeting();
exchangeService.OnSerializeCustomSoapHeaders -= service_OnSerializeCustomSoapHeaders;
Event Implementation:
static void service_OnSerializeCustomSoapHeaders(XmlWriter writer)
{
writer.WriteRaw(Environment.NewLine + " <t:TimeZoneContext><t:TimeZoneDefinition Id=\"" + TimeZoneInfo.Local.StandardName + "\"/></t:TimeZoneContext>" + Environment.NewLine);
}
I am trying to add appointments fetched from the database. I fetch them "manually". I store them in a datatable called dt. The data is present in the datatable.
foreach (Resource r in schedulerStorage1.Resources.Items)
{
Console.WriteLine(r.Caption + " - " + r.Id);
}
Appointment app;
foreach (DataRow dr in result.DataTable_Result.Rows)
{
Console.WriteLine(dr[0] + " - " + dr[1] + " - " + dr[2]);
app = schedulerControl1.Storage.CreateAppointment(AppointmentType.Normal);
app.ResourceId = dr[0];
app.Start = DateTime.Parse(dr[1].ToString());
app.End = DateTime.Parse(dr[2].ToString());
schedulerControl1.Storage.Appointments.Add(app);
}
Output of the Console.WriteLines:
Room1 - 1
Room2 - 2
1 - 16/01/2013 8:00:00 AM - 16/01/2013 8:05:00 AM
1 - 16/01/2013 9:00:00 AM - 16/01/2013 9:05:00 AM
2 - 16/01/2013 8:00:00 AM - 16/01/2013 8:30:00 AM
Any ideas about why the appointments are not added ?
Alright I managed to find out the source of the problem.
dr[0] is the resource id.
When I added a resource I wrote:
r = new Resource(dr[0], dr[1].ToString());
When I added an appointment I wrote:
app.ResourceId = dr[0];
The following will fix the appointments not showing up.
r = new Resource(dr[0].ToString(), dr[1].ToString());
app.ResourceId = dr[0].ToString();
Now everything works perfectly.
Thank you Aphelion for your help. :D
I need the current appointment. If no current appointment then the next or even previous appointment.
I figure to use Restrict to limit the set of appointments, and then choose either the first or last appointment depending on the restrict argument (e.g. Restrict to appointments ending after current time, or appointments starting before current time).
I'm having trouble with the string filter needed as argument.
A simple VB example (code stump):
myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM")
strRestriction = "[Start] <= '" & myStart & "'"
'Restrict the Items collection
Set oResItems = oItems.Restrict(strRestriction)
'Sort
oResItems.Sort "[Start]"
I am attempting to do the same in C#.
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace and Logon information.
// Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
//Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Get the Calendar folder.
Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
// Get the Items (Appointments) collection from the Calendar folder.
oItems = oCalendar.Items;
oItems.IncludeRecurrences = true;
// THIS IS THE PROBLEM AREA
String filter = "[Start] <= '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") + "'";
Outlook.Items restrictedItems = oItems.Restrict(filter);
// Take the last item on the list - should be current or next appointment
restrictedItems.Sort("[Start]");
Outlook.AppointmentItem oAppt = restrictedItems.GetLast();
// Done. Log off.
oNS.Logoff();
I imagine since the filter is a string, the date format needs to be yyyy/mm/dd HH:mm:ss? I can't find any documentation on how to manipulate the [Start], like parsing it to a date or something.
Depending on the date format, I will either get the wrong appointment, or will be unable to use GetLast due to the filter excluding all appointments.
I've seen examples, but either they loop through the appointments (too inefficient), or the date formats look like they can't be trusted to return the correct appointment (For example https://social.msdn.microsoft.com/Forums/vstudio/en-US/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-appointment-items-from-outlook-2007-calendar-restrict?forum=vsto, which seems to have the date hardcoded instead if using DateTime.Now.)
UPDATE: I'm currently looping through like shown below. Any suggestions for more efficient code?
DateTime currentTime = DateTime.Now;
foreach (Outlook.AppointmentItem item in oItems)
{
if (item.Start <= currentTime && item.End.Subtract(new TimeSpan(0, 10, 0)) > currentTime)
{
appointmentArrayList.Add(item);
}
}
This is your issue:
DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM")
What I think you're going for is:
DateTime.Now.ToString("MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture)
By following the information found here, I was able to get it to work using "yyyy-MM-dd HH:mm" as the format string for the toString call.
Hope this helps.
This code works to show Outlook appointments from today onwards:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DemoAppointmentsInRange();
}
private void DemoAppointmentsInRange()
{
Application a = new Application();
Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder;
DateTime start = DateTime.Now;
DateTime end = start.AddDays(5);
Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts)
{
Response.Write("Subject: " + appt.Subject + " "+" Start: "+appt.Start.ToString()+" "+"End:"+appt.End.ToString()+"<br/>");
}
}
}
private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange(
Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime)
{
string filter = "[Start] >= '"+ startTime.ToString("g")+ "' AND [End] <= '" + endTime.ToString("g") + "'";
//Response.Write(filter);
try
{
Microsoft.Office.Interop.Outlook.Items calItems = folder.Items;
calItems.IncludeRecurrences = true;
calItems.Sort("[Start]", Type.Missing);
Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter);
if (restrictItems.Count > 0)
{
return restrictItems;
}
else
{
return null;
}
}
catch
{
return null;
}
}
}
I couldn't figure out DateTime format, but I found this article:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering
Then I realized that they have a concept called "DASL Queries", reading furthermore I found this related to filtering with DateTime:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering-items-using-a-date-time-comparison
And I've decided to get all today's AppointmentItem(s) this way:
public enum MacroName
{
today,
tomorrow,
yesterday,
next7days,
last7days,
nextweek,
thisweek,
lastweek,
nextmonth,
thismonth,
lastmonth
}
private Outlook.Items GetAppointmentsWithMacro(Outlook.Folder folder, MacroName macro)
{
string strFilter = "#SQL=%" + macro.ToString() + "(\"urn:schemas:calendar:dtstart\")%";
try
{
Outlook.Items calItems = folder.Items;
calItems.IncludeRecurrences = true;
calItems.Sort("[Start]", Type.Missing);
Outlook.Items restrictItems = calItems.Restrict(strFilter);
if (restrictItems.Count > 0)
{
return restrictItems;
}
else
{
return null;
}
}
catch { return null; }
}
I want to be safest as possible because it seems that filtering by DateTime is related to local DateTime settings. By using the "today" macro I was able for the first time to extract the correct information from Outlook appointments store