How to Accept and add Categories to RequiredAttendees Appointments using Exchange Web Services - vb.net

I’m using ExchangeService(ExchangeVersion.Exchange2010_SP1)
I want to Accept and add Categories to RequiredAttendees Appointments. To do this i need to find these Appointments.
My understanding in EWS is that on Save of an Appointment that has RequiredAttendees a new Meeting Request is created for each of the ‘required attendees’.
How can I access the Appointments that were created automatically for the ‘required attendees’? These are shown in the required attendees Calendars as Appointments, along with a Meeting Request.
I have managed to do a crude find on the Subject (steps below)
Connect to server as Organiser
Create Appointment
Set Subject
Add Required Attendee
Save Appointment
Connect to server as Required Attendee from step 4
Find Appointment that has Subject at step 3
Add Categories to Appointment at step 7
Update Appointment at step 7
Accept Appointment at step 7
And this does work, but concerned users will change the Subject.
I have tried adding an Extended Property and value to the Appointment created by the Organiser and then FindItems for the Extended Property value in the Appointments connected as the Required Attendee. This does not work.
Is there a preferred method for what I’m trying to accomplish?
Thank you
Private Shared ReadOnly m_organiserEmailAddress As String = "Organiser#test.com"
Private Shared ReadOnly m_eventIdExtendedPropertyDefinition As New ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, "EventId", MapiPropertyType.Long)
'--> start here
Public Shared Function SaveToOutlookCalendar(eventCalendarItem As EventCalendarItem) As String
If eventCalendarItem.Id Is Nothing Then
'new
Dim newAppointment = EventCalendarItemMapper.SaveNewAppointment(eventCalendarItem)
'set the Id
eventCalendarItem.Id = newAppointment.Id.UniqueId.ToString()
'accept the calendar item on behalf of the Attendee
EventCalendarItemMapper.AcceptAppointmentAsAttendees(newAppointment)
Return eventCalendarItem.Id
Else
'update existing appointment
Return EventCalendarItemMapper.UpdateAppointment(eventCalendarItem)
End If
End Function
Private Shared Sub ConnectToServer(Optional autoUser As String = "")
If autoUser = "" Then
_service.Url = New Uri(ExchangeWebServicesUrl)
Else
_service.AutodiscoverUrl(autoUser)
End If
End Sub
Private Shared Sub ImpersonateUser(userEmail As String)
_service.Credentials = New NetworkCredential(ImpersonatorUsername, ImpersonatorPassword, Domain)
_service.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail)
End Sub
Private Shared Function SaveNewAppointment(eventCalendarItem As EventCalendarItem) As Appointment
Try
ConnectToServer(m_organiserEmailAddress)
ImpersonateUser(m_organiserEmailAddress)
Dim appointment As New Appointment(_service) With {
.Subject = eventCalendarItem.Subject}
'add attendees
For Each attendee In eventCalendarItem.Attendees
appointment.RequiredAttendees.Add(attendee.Email)
Next
'add categories
For Each category In eventCalendarItem.Categories
appointment.Categories.Add(Globals.GetEnumDescription(category))
Next
'add EventId = 5059 as an extended property of the appointment
appointment.SetExtendedProperty(m_eventIdExtendedPropertyDefinition, 5059)
appointment.Save(SendInvitationsMode.SendOnlyToAll)
Return appointment
Catch
Throw New Exception("Can't save appointment")
End Try
End Function
Private Shared Sub AcceptAppointmentAsAttendees(appointment As Appointment)
For Each attendee In appointment.RequiredAttendees
Try
ConnectToServer(attendee.Address.ToString())
ImpersonateUser(attendee.Address.ToString())
For Each a In FindRelatedAppiontments(appointment)
a.Categories.Add(Globals.GetEnumDescription(CalendarItemCategory.Workshop))
a.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone)
a.Accept(True)
Next
Catch
Throw
End Try
Next
End Sub
Private Shared Function FindRelatedAppiontments(appointment As Appointment) As List(Of Appointment)
Dim view As New ItemView(1000)
Dim foundAppointments As New List(Of Appointment)
view.PropertySet =
New PropertySet(New PropertyDefinitionBase() {m_eventIdExtendedPropertyDefinition})
'Extended Property value = 5059
Dim searchFilter = New SearchFilter.IsEqualTo(m_eventIdExtendedPropertyDefinition, 5059)
For Each a In _service.FindItems(WellKnownFolderName.Calendar, searchFilter, view)
If a.ExtendedProperties.Count > 0 Then
foundAppointments.Add(appointment.Bind(_service, CType(a.Id, ItemId)))
End If
Next
Return foundAppointments
End Function

Well one thing is for sure that there is nothing straight forward in the EWS.
I will be honest with that, that till the moment, I did not work with integrating from the interior calendar to the Exchange calendar, my experience is the opposite , which is get what in the exchange to the internal one.
Anyway after reading your code, I think you are almost there. However I suggest that you catch the appointments that reach to the attendee by using the Streaming Notifications it is not that hard!
So I would say the steps should be like this
Create the appointment
Apply The extended property thing ( I suggest to use the GUID instead of hard coded number) as follows
Create an extended property, and put guid for the appointment, and it wont change unless you made a copy from another appointment (after all it is just a property)
private static readonly PropertyDefinitionBase AppointementIdPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "AppointmentID", MapiPropertyType.String);
public static PropertySet PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointementIdPropertyDefinition);
//Setting the property for the appointment
public static void SetGuidForAppointement(Appointment appointment)
{
try
{
appointment.SetExtendedProperty((ExtendedPropertyDefinition)AppointementIdPropertyDefinition, Guid.NewGuid().ToString());
appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone);
}
catch (Exception ex)
{
// logging the exception
}
}
//Getting the property for the appointment
public static string GetGuidForAppointement(Appointment appointment)
{
var result = "";
try
{
appointment.Load(PropertySet);
foreach (var extendedProperty in appointment.ExtendedProperties)
{
if (extendedProperty.PropertyDefinition.Name == "AppointmentID")
{
result = extendedProperty.Value.ToString();
}
}
}
catch (Exception ex)
{
// logging the exception
}
return result;
}
Catch the appointment with the StreamingNotificationEvent. In my opinion a good way to do that is to run both Organizer and Attendee in the same time and catch the appointments between them.
To see an example, I have posted an answer to a previous question Multiple impersonation-threads in Exchange Web Service (EWS) . Please vote for the answers of both posts (here and there) if you found my answers are useful.
I do not want to make you scared, but once you solve your current problem; if you want to continue with it will get more complicated. I could write down how I solved the meetings problem, but I do not see it straight forward at all so it might be better if you write your own.
Cheers

Related

ActiveDocument.ActiveWindow.Hwnd crash

I'm using ActiveDocument.ActiveWindow.Hwnd to get the handle of the active document Window in MS Word, but it does crash in Word 2007. I tried using long instead of int, tried to catch the exception but no exception is thrown.
Public WithEvents wa As Microsoft.Office.Interop.Word.Application
wa = HostApplication
Public Function getWindowHWND() As Integer
Try
getWindowHWND = wa.ActiveDocument.ActiveWindow.Hwnd
Catch ex As Exception
MsgBox(1)
getWindowHWND = -1
Finally
End Try
End Function
Am I doing something wrong? Is this a bug? How can I get the handle in an equivalent way if it's not fixable? Thank you in advance.
No such property exist in Word 2007.
Since Word 2013 you could use the Hwnd property of Window that is exposed from the Application object:
var windowHandle = wordApplication.ActiveWindow.Hwnd;
There is no need to use the ActiveDocument property in the middle. This fails when no document is open.
In earlier Word versions you could use the Process class from the .NET BCL to retrieve the main window handle, for example:
var word = new Microsoft.Office.Interop.Word.Application();
word.Visible = true;
word.Activate();
word.Application.Caption = "My Word";
foreach( Process p in Process.GetProcessesByName( "winword" ) )
{
if( p.MainWindowTitle == "My Word" )
{
Debug.WriteLine( p.Handle.ToString() );
}
}

VSTO Outlook Plugin: Cannot get AppointmentItem in Item_Change event when recurring appointment is dragged and dropped by user

I want to catch the change event on an AppointmentItem. I use Outlook 2017 for tests.
To achieve I use:
I attached the events like this:
public void AttachEvents()
{
_CalendarItems.ItemAdd += Item_Add;
_CalendarItems.ItemChange += Item_Change;
_DeletedItems.ItemAdd += Item_Delete_Add;
The Item_Change method looks like this:
public void Item_Change(Object item)
{
if (item != null && item is Outlook.AppointmentItem)
{
Outlook.AppointmentItem myAppointment = item as Outlook.AppointmentItem;
To test the code I created a recurring appointment series. I double-clicked on appointment in the calendar and entered some title and body and saved.
Now I started my code and inspected the item.
Unfortunately, item points to the series and NOT to the individual appointment when item changed is initiated.
How can I retrieve the actual AppointmentItem when Item_Changed is initiated?
Related Stackoverflow Posting: Outlook Addin: Moving Appointment in Calendar does not reflect new date/time in AppointmentItem (catch Calendar.ItemChange) But still there is no solution to this
More on this topic:
https://www.add-in-express.com/forum/read.php?FID=5&TID=15384
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4ec55891-fb64-408f-b1cf-4bf05765b866/outlook-get-original-time-of-recurring-exception-item-that-is-opened-with-drag-drop?forum=vsto
Exceptions are not actual appointments - they are stored as embedded message attachments on the master appointments. You get the master appointment, and you would need to access its exceptions to see what changed.
There is a tricky solution to that.
Assumption: The user uses the calendar view to change the item. As the event is thrown by the calendar view this should be true in all cases:
_CalendarItems = calendarFolder.Items;
_CalendarItems.ItemChange += Item_Change;
[...]
now we can use the CalendarView to calculate the selected Startdate and compare it to all Exceptions stored in the RecurrencePattern ...
if (myAppointment.IsRecurring)
{
// in case of recurring appointments at this point we always get
// only a reference to the series master NOT the occurrence
// Assumption: The user clicked on the AppointmentItem in the calendar view
// So we can calculate the selected Start Time from this selection range
// then compare this against all Exceptions in the OccurrencePattern of the recurring pattern
// if we find one AppointmentItem in the Exceptions which has the same DateTime then we found the correct one.
//
Outlook.Application application = new Outlook.Application();
Outlook.Explorer explorer = application.ActiveExplorer();
Outlook.Folder folder = explorer.CurrentFolder as Outlook.Folder;
Outlook.View view = explorer.CurrentView as Outlook.View;
// get the current calendar view
if (view.ViewType == Outlook.OlViewType.olCalendarView)
{
Outlook.CalendarView calView = view as Outlook.CalendarView;
Outlook.RecurrencePattern pattern = myAppointment.GetRecurrencePattern();
for (int i = 1; i <= pattern.Exceptions.Count; i++)
{
Outlook.Exception myException = pattern.Exceptions[i];
Outlook.AppointmentItem exceptionItem = myException.AppointmentItem;
DateTime itemDateStart = exceptionItem.Start;
if (itemDateStart == calView.SelectedStartTime)
{
updateMyPluginMeeting(exceptionItem);
return; // the use may only select on AppointmentItem so we can skip the rest
}
}
}
}
If you know any better solution to this let me know.

Email sent from C# OOM stays in Outbox if Outlook is closed until next Outlook start

I'm trying to send emails from a .NET application using Outlook Object Model.
My application displays the Outlook message window so the user can see what we're sending and edit it first. When the user hits the Send button, the Outlook window closes, and the message gets sent. This works perfectly as long as the Outlook application is already running.
If the Outlook application isn't already running, the message gets stuck in the Outbox, and will not send until I start Outlook. When I start Outlook, I can see the message sitting in the Outbox folder for a few seconds, then it gets sent.
I need to show the New Message form to Outlook user to select the recipient(s) and possibly edit the message before sending.
Note: I know that this question was already asked here Email sent with Outlook Object Model stays in Outbox until I start Outlook
and the solution exists, but it is not provided (only the small hint is provided) and unfortunately I cannot ask for clarification / code example because I have not enough "reputation".
I tried to write my own implementation of the hint provided, but the SyncEnd event is fired only when Outlook is already open (just to remind, the question is about the case then Outlook is closed).
My code below. What is wrong?
using Microsoft.Office.Interop.Outlook;
using OutlookApp = Microsoft.Office.Interop.Outlook.Application;
class Mailer
{
AutoResetEvent mailSentEvent = new AutoResetEvent(false);
public void CreateMail()
{
OutlookApp outlookApp = null;
MailItem mailItem = null;
try
{
outlookApp = new OutlookApp();
mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "Test Message";
mailItem.Body = "This is the message.";
string reportPath = #"C:\temp\aaaaa.pdf";
mailItem.Attachments.Add(reportPath);
mailItem.Display(true);
StartSync(outlookApp);
bool result = mailSentEvent.WaitOne();
}
catch (System.Exception)
{
throw;
}
finally
{
if (mailItem != null) Marshal.ReleaseComObject(mailItem);
if (outlookApp != null) Marshal.ReleaseComObject(outlookApp);
}
}
private static SyncObject _syncObject = null;
private void StartSync(OutlookApp outlookApp)
{
var nameSpace = outlookApp.GetNamespace("MAPI");
_syncObject = nameSpace.SyncObjects[1];
_syncObject.SyncEnd += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(OnSyncEnd);
_syncObject.Start();
}
private void OnSyncEnd()
{
mailSentEvent.Set();
}
}
the SyncEnd event is fired only when Outlook is already open
That is not true. The SyncObjects collection contains all Send\Receive groups. You need to iterate over all objects in the collection and call the Start method, for example:
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox("Do you wish to synchronize " &; syc.Name &;"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next

Primefaces - Use an Object in the DefaultScheduleEvent <p:schedule>

I'm using the <p:shecule> and i want to know how can i interact or use this Object data DefaultScheduleEvent.
For example if i want to fill the calendar with more information or save different data than the String title, Date start, date End.
Right now i'm filling the calendar using a for. And it's working fine. But there are more related information that i want to show in the <p:dialog>.
This is the addition information that i want to save/show
for (TimeSheetEntity timeSheetEntity : listaReportesTS) {
String descripcion = timeSheetEntity.getProyectoE().getDescripcionProyecto();
Date fechaInicio = obtFechaHora(timeSheetEntity.getDiaLaboral(), timeSheetEntity.getHoraInicio());
Date fechaFinal = obtFechaHora(timeSheetEntity.getDiaLaboral(), timeSheetEntity.getHoraFin());
eventModel.addEvent(new DefaultScheduleEvent(descripcion, fechaInicio, fechaFinal));
}
How can i do it? and how can i save all this data and get the DefaultScheduleEvent recognize it?
Thank you!
EDIT 1:
I created a new class that extends DefaultScheduleEvent but still no luck. The event is only saved when i enter the title, the start date and the end date. If i enter any other value nothing happens, not even an error in the console.
public class Prueba extends DefaultScheduleEvent{
private AsignacionEntity proyecto;
private TipoActividadEntity actividad;
private boolean horaAlmuerzo;
private String desc;
private String incidente;
private float horasTrabajadas;
public Prueba() {
super();
}
public Prueba(String title, Date startDate, Date endDate) {
super(title, startDate, endDate);
}
//getters and setters
And in the bean:
private Prueba pruebaEvento = new Prueba();
In the view:
<h:outputText for="incidente" value="IM/ALM"/>
<p:inputText id="incidente" value="#{registroBean.pruebaEvento.incidente}" required="false" size="5"/>
Finally i tried with the Object data and set the value from the VO/DTO in the constructor but no luck again.
dse = new DefaultScheduleEvent();
dse.setData(new TimeSheetVO());

How do I check whether a Linq-to-SQL object is already attached to a DataContext?

I have an object that may have been inflated via a plain old DataContext, or may just have been new-ed up with just its .ID property set. There's no way to know for sure. I'm looking to rehydrate the entire object from whatever is in the database. If the object was new-ed up, I can call .Attach() on the table object and refresh from the Data Context with no trouble. But, if the object was already inflated from the DataContext I get the error: "Cannot attach an entity that already exists.". There's no timestamp field or anything like that - just an integer primary key being used to control the rehydration. I'd like to know if there's a way to conditionally attach. Here's the code - it works the way I want it to, but this seems a hackish way to go about it:
' myDC is a shared instance of a vanilla DataContext...
' myObj is an instance of a linqed-up `SomeLinqObject`
Dim tbl = myDC.GetTable(Of SomeLinqObject)()
Try
tbl.Attach(myObj) ' <-- Wish I could just TryAttach() here!
Catch ex As Exception
If ex.Message = "Cannot attach an entity that already exists." Then
' Do nothing
Else
Throw
End If
End Try
myDC.Refresh(RefreshMode.OverwriteCurrentValues, myObj) ' Rehydrate
-- EDIT --
Thanks to Isaac's answer, here's the revised code:
Dim tbl = myDC.GetTable(Of SomeLinqObject)()
Dim isAttached = (tbl.GetOriginalEntityState(myObj) IsNot Nothing)
If Not isAttached Then tbl.Attach(myObj)
myDC.Refresh(RefreshMode.OverwriteCurrentValues, myObj) ' Rehydrate
GetOriginalEntityState(T entity) on Table -might- be what you're looking for. If you pass it an entity that you've loaded from the context, it returns the original version of the entity held in the context. If you pass it a new entity (or I believe one simply not sourced from that context), it returns null.
var context = new DataClasses1DataContext();
var person = context.Person.First();
var isAttachedToContext = context.Person.GetOriginalEntityState(person) != null; // returns true
var isNewEntityAttachedToContext = context.Peoples.GetOriginalEntityState(new Person()) != null; // returns false
Apologies - answer is in C# but I hope you get the gist!