C# outlook interop saving meeting as appointment issues - c#

I am trying to save an Outllok meeting ics file as an Outlook appointment ics but when i use do SaveAs to a folder location, it is saving as a meeting.
I have the following code but I can not seem to locate the issue. When I use .Display it shows as an appointment but .SaveAs is saving back as a meeting with recipients.
Any help is much appreciated
Outlook.Application App = new Outlook.Application();
string FPath = #"C:\\Documents\TestMeeting.ics"
var item = App.Session.OpenSharedItem(FPath) as Outlook.MeetingItem;
var AppointmentItem = item.GetAsssociatedAppointment(false);
AppointmentItem.MeetingStatus = Outlook.OlMeetingStatus.olNonMeeting;
AppointmentItem.Display(true);
AppointmentItem.SaveAs(#"C:\\Documents\TestAppointment1.ics",Outlook.OlSaveAsType.olIcal);

Use the Save method to save the just opened meeting into your calendar:
string FPath = #"C:\\Documents\TestMeeting.ics"
var item = App.Session.OpenSharedItem(FPath) as Outlook.MeetingItem;
item.Save();
// only after that you can try reprieving the associated appointment
var AppointmentItem = item.GetAsssociatedAppointment(false);
Also I'd recommend creating a new appointment item from scratch and then saving it to the disk.

Thanks Eugene. I managed to get this working by creating a new appointment and using .BodyFormat and .RTFBody of the associated appointment. I have included the full code below for anyone in the future and marked your answer as correct. Apologies but at present I don't have enough rep to upvote otherwise I would. Many thanks
Outlook.Application App = new Outlook.Application();
string FPath = #"C:\\Documents\TestMeeting.ics"
var item = App.Session.OpenSharedItem(FPath) as Outlook.MeetingItem;
var AppointmentItem = item.GetAsssociatedAppointment(false);
Outlook.AppointmentItem ap1 = (Outlook.AppointmentItem)App.CreateItem(Outlook.OlItemType.olAppointmentItem);
ap1.Start = AppointmentItem.Start;
ap1.Subject = AppointmentItem.Subject;
ap1.BodyFormat = AppointmentItem.BodyFormat;
ap1.RTFBody = AppointmentItem.RTFBody;
ap1.Location = AppointmentItem.Location;
ap1.SaveAs(#"C:\\Documents\TestAppointment1.ics",Outlook.OlSaveAsType.olIcal);

Related

Is it possible to directly open the Outlook meeting window?

I am trying to have my application to open the Outlook meeting window with some pre-populated fields.
I have found that this question was already asked here.
However, the code provided in the answer(which works fine) doesn't open the meeting window but the appointement window. Those are two different things that are handled differently in Outlook and what I need is indeed the meeting window.
Is there any way to achieve this or do I absolutely have to open the appointement window first and then invite people to turn it into a meeting?
Create an appointment just as in the other question, but then set the MeetingStatus property of the appointment.
Microsoft.Office.Interop.Outlook.Application outlookApplication = new Microsoft.Office.Interop.Outlook.Application(); ;
Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
// This line was added
appointmentItem.MeetingStatus = Microsoft.Office.Interop.Outlook.OlMeetingStatus.olMeeting;
appointmentItem.Subject = "Meeting Subject";
appointmentItem.Body = "The body of the meeting";
appointmentItem.Location = "Room #1";
appointmentItem.Start = DateTime.Now;
appointmentItem.Recipients.Add("test#test.com");
appointmentItem.End = DateTime.Now.AddHours(1);
appointmentItem.ReminderSet = true;
appointmentItem.ReminderMinutesBeforeStart = 15;
appointmentItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
appointmentItem.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
appointmentItem.Recipients.ResolveAll();
appointmentItem.Display(true);
One more note to NineBerries good solution, because I had an issue here:
The line
appointmentItem.Recipients.ResolveAll();
is necessary if you have optional attendees in the meeting.
Otherwise they will be reset to "required" even if you set
recipient.Type = Microsoft.Office.Interop.Outlook.OlMeetingRecipientType.olOptional;
before, which is due to "late auto-resolving" of names in Outlook (or so it seems).

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 :)

Create appointment with custom properties in EWS

I try to add a custom property to created appointments like this:
var newEvent = new Appointment(service)
{
Start = start,
End = end,
Subject = subject,
ReminderMinutesBeforeStart = 15
};
var extendendProperty = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Address, "organizer",
MapiPropertyType.String);
newEvent.SetExtendedProperty(extendendProperty, organizer);
but problem is that when I try get this appointment from server, property ExtendedProperty is empty.
In addition I create new appointment and add 'room' as a required attendee, and when I try get this appointment, I don't get it from my calendar but from room calendar.
So, I want to add extend property to my appointment and invite 'room'. Next get all appointments of the room and here I want read this property. It is even possible?
I read this topic: EWS Create Appointment in exchange with extra custom properties and as I understand I'll must have access to ExtendendPropertyDefinition when I want read this property, and must known id of this appointment before. Now I download all appointments from outlook by this code:
var folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(userName));
var calendar = CalendarFolder.Bind(service, folderId, new PropertySet());
return calendar.FindAppointments(new CalendarView(start, stop)).ToList();
EDIT
Thanks Glen Scales!
It almost works as I want, but one thing. I can read this additional property if I download my own appointments, but in that code I download appointments from room calendar.
As I suppose when creating new appointment and add room as required attendant, it create his own appointment and this additional property isn't copied.
So is any way to get this additional property from room appointment, when I add this property to my?
You need to first Create a property set, add the extended property you want to load to that property set. Then tell EWS you want that property returned when you execute the FindAppointment method see https://msdn.microsoft.com/en-us/library/office/dd633697(v=exchg.80).aspx eg in your example
PropertySet YourProperyset = new PropertySet(BasePropertySet.FirstClassProperties);
var extendendProperty = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Address, "organizer",MapiPropertyType.String);
YourProperyset.Add(extendendProperty);
var folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(userName));
var calendar = CalendarFolder.Bind(service, folderId);
var calendarView = new CalendarView(start, stop);
calendarView.PropertySet = YourProperyset;
return calendar.FindAppointments(calendarView).ToList();

Adding appointment to Outlook (2013) opens up the meeting editor

I am trying to add appointments to Outlook programmatically.
I ran this code which runs successful but after I save the appointment the meeting editor opens up in outlook.
AppointmentItem appItem = null;
try
{
appItem = outlookItems.Add(OlItemType.olAppointmentItem) as AppointmentItem;
if(appItem == null)
continue;
appItem.Subject = "Subject";
appItem.MeetingStatus = OlMeetingStatus.olMeeting;
appItem.Location = "Location";
appItem.Save();
appItem.Display(true);
}
finally
{
if (appItem != null)
{
Marshal.ReleaseComObject(appItem);
}
}
I tried calling Display(true), Display(false) it still doesn't work.
Please can anyone tell me if I am doing anything wrong.
But you create a new meeting item in the code setting the following property:
appItem.MeetingStatus = OlMeetingStatus.olMeeting;
If you don't want to see a new item window (inspector), there is no need to run the following line of code:
appItem.Display(true);
The Display method displays a new Inspector object for the item.
You may find the Getting Started with VBA in Outlook 2010 article in MSDN helpful.
if the appointment is of type OlMeetingStatus.olMeeting, recipients are supposed to be present.
I changed the type to
appItem.MeetingStatus = OlMeetingStatus.olNonMeeting
and removed the call to display. I was able to save the appointment in the calendar

Outlook appointment on Server not working

I have created Following function to set Outlook appointment. It's work fine while running in localhost with visual Studio Editor.
But when I upload publish files on server its not working.
Do I need to set anything in server for Outlook or need to include any additional dll/files?
Any help will be appreciated. Pls let me know, if need more information.
public void OutLookReminder(string DateTimeVal)
{
Outlook.Application outlookApp = new Outlook.Application(); // creates new outlook app
Outlook.AppointmentItem oAppointment = (Outlook.AppointmentItem)outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem); // creates a new appointment
oAppointment.Subject = "........Subject"; // set the subject
oAppointment.Body = "--------Body"; // set the body
oAppointment.Location = "-------Location"; // set the location
oAppointment.Start = DateTime.ParseExact(DateTimeVal, "dd/MM/yyyy H:mm",null); // Set the start date
oAppointment.End = DateTime.ParseExact(DateTimeVal, "dd/MM/yyyy H:mm", null); // End date
oAppointment.ReminderSet = true; // Set the reminder
oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
oAppointment.Importance = Outlook.OlImportance.olImportanceHigh; // appointment importance
oAppointment.BusyStatus = Outlook.OlBusyStatus.olBusy;
// save the appointment
oAppointment.Save();
}
Do you mean you are running this under a service (such as IIS)? You cannot do that.
Where is the appointment supposed to be created? Current user who is using your code in a browser (then your code needs to run locally in JavaScript)?

Categories