EWS - Get a custom header - c#

Hello I am using the following code to get the custom header from EWS.
But unfortunately it's not returning the header. I looked into the outlook for the headers using Mapi tool, where I can see the headers.
Any suggestions please.
service = ExchangeServiceHelpers.GetBinding();
// Bind the Inbox folder to the service object
var inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
var searchFilter = ExchangeServiceHelpers.PopulateSearchFilters();
var view = new ItemView(int.MaxValue); // Search operation should return maximum number of elements.
// Defines a property set that contains the schematized Internet message headers.
var headerProperty = new ExtendedPropertyDefinition(
DefaultExtendedPropertySet.InternetHeaders,
"x-worksitefolderemailid",
MapiPropertyType.String);
var columns = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId, headerProperty);
view.PropertySet = columns;
// Fire the query for the unread items
var findResults = inbox.FindItems(searchFilter, view);
// Loop through the search results.
foreach (EmailMessage message in findResults)
{
try
{
message.Load(
new PropertySet(new PropertyDefinitionBase[] { ItemSchema.MimeContent, ItemSchema.Subject}));
string mailAddress = GetFolderId(message, headerProperty); // Get internet header
if (string.IsNullOrEmpty(mailAddress))
{
Logger.Info(
string.Format("Email '{0}' doesn't have folder id address. Marking as Read Item.",
message.Subject));
ExchangeServiceHelpers.MarkMessageAsRead(service, message.Id); // Marking the email item as Read prevents the item to be returned in further search results.
continue;
}
}
catch (Exception e)
{
Logger.Error(e);
}
}
private static string GetFolderId(EmailMessage message, ExtendedPropertyDefinition headerProperty)
{
try
{
if (message.ExtendedProperties == null || message.ExtendedProperties.Count == 0)
{
Logger.Info(
string.Format("Email '{0}' doesn't have any extended properties. Marking as Read Item.",
message.Subject));
return string.Empty;
}
//message.InternetMessageHeaders
foreach (ExtendedProperty property in message.ExtendedProperties)
{
if (property.PropertyDefinition == headerProperty)
{
return property.Value.ToString();
}
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
return string.Empty;
}

Naresh,
The inbox.FindItems() call won't return the internet headers. You'll need to update message.Load() to use a property set that includes headerProperty.
With regards,

Related

SharePoint CSOM not throwing expected exceptions on ExecuteQuery

Essentially what the title says, for whatever reason when I call clientContext.ExecuteQuery(); and for example the list the data is going into has a missing column, I don't get an exception, it actually just runs as expected and I have to go and investigate what might have caused the data to not appear.
The NuGet package i'm using is Microsoft.SharePoint2016.CSOM version 16.0.4690.1000
Any hints, or suggestions to point me in the right direction appreciated. It's entirely possible I'm being a bit dim here.
Here's the full code block I'm using for updating list items:
public override object UpdateEntity(object entity)
{
if (entity == null)
{
// if the definition is null throw argument null exception.
throw new ArgumentNullException(nameof(SharePointDefinition));
}
// check for incorrect type being passed in that we can still handle
if (entity is List<SharePointDefinition> definitions)
{
return UpdateEntities(definitions);
}
ExceptionHandlingScope exceptionScopeFetch = new ExceptionHandlingScope(clientContext);
ExceptionHandlingScope exceptionScopeSubmit = new ExceptionHandlingScope(clientContext);
// run single definition submit to SP.
if (entity is SharePointDefinition definition)
{
// variables.
IntegrationEventLog log = new IntegrationEventLog();
EventInformation ei = new EventInformation();
List list = null;
ListItemCollection listItemCol = null;
using (exceptionScopeFetch.StartScope())
{
using (exceptionScopeFetch.StartTry())
{
// get the required list
list = clientContext.Web.Lists.GetByTitle(definition.ListName);
// create query
listItemCol = list.GetItems(CamlQuery.CreateAllItemsQuery());
// set these items for retrieval.
clientContext.Load(listItemCol);
}
using (exceptionScopeFetch.StartCatch())
{
// Assume that if there's an exception, it can only be
// because there is no list with the specified title, so report this back.
if (exceptionScopeFetch.HasException)
{
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentFailure,
LoggingSeverity = LoggingSeverity.HighSeverity,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = $"Hit SharePoint exception handler during list and data pull: Message: {exceptionScopeFetch.ErrorMessage}",
StackTrace = exceptionScopeFetch.ServerStackTrace,
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Error;
log.EventID = LoggingSeverity.HighSeverity;
log.Message = JsonConvert.SerializeObject(ei);
AddToLog(log);
}
}
using (exceptionScopeFetch.StartFinally())
{
//
}
}
// get item instances first
try
{
clientContext.ExecuteQuery();
}
catch (Exception genEx)
{
// return failure log.
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentFailure,
LoggingSeverity = LoggingSeverity.HighSeverity,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = "Errored trying to get data from SharePoint list ready for update operation; see stacktrace for more information.",
StackTrace = genEx.StackTrace,
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Error;
log.EventID = LoggingSeverity.HighSeverity;
log.Message = JsonConvert.SerializeObject(ei);
AddToLog(log);
return false;
}
// this is the column we want to overwrite.
var comparisonColumn = definition.UpdateIdentifier ?? "";
//List col to dict
var listItems = listItemCol.Cast<ListItem>().ToList();
// Now we know if we were able to retrieve existing data, perform submit.
using (exceptionScopeSubmit.StartScope())
{
using (exceptionScopeSubmit.StartTry())
{
// loop through our rows
foreach (var row in definition.RowData)
{
int existingItemIndex = -1;
// see if the row exists already
if (!string.IsNullOrEmpty(comparisonColumn) && listItems.Count != 0)
{
existingItemIndex = listItems.FindIndex(x => x[comparisonColumn].ToString() == row[comparisonColumn]);
}
if (existingItemIndex != -1 && listItems.Count != 0)
{
// item exists - loop through our row columns
foreach (var keyValuePair in row)
{
// they key relates to a column, the Value to the rows colum Value.
listItems[existingItemIndex].ParseAndSetFieldValue(keyValuePair.Key, keyValuePair.Value);
}
// update this item
listItems[existingItemIndex].Update();
}
else
{
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = list.AddItem(itemCreateInfo);
// loop through our row columns
foreach (var keyValuePair in row)
{
// they key relates to a column, the Value to the rows colum Value.
newItem[keyValuePair.Key] = keyValuePair.Value;
}
newItem.Update();
}
}
}
using (exceptionScopeSubmit.StartCatch())
{
// Assume that if there's an exception, it can only be
// because there is no list with the specified title, so report this back.
if (exceptionScopeFetch.HasException)
{
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentFailure,
LoggingSeverity = LoggingSeverity.HighSeverity,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = $"Error at SharePoint exception handler during submit to list: Message: {exceptionScopeFetch.ErrorMessage}",
StackTrace = exceptionScopeFetch.ServerStackTrace,
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Error;
log.EventID = LoggingSeverity.HighSeverity;
log.Message = JsonConvert.SerializeObject(ei);
AddToLog(log);
}
}
using (exceptionScopeSubmit.StartFinally())
{
//
}
}
// try to execute submit.
try
{
clientContext.ExecuteQuery();
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentSuccess,
LoggingSeverity = LoggingSeverity.Information,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = "No exceptions were thrown from the Execution process.",
StackTrace = "",
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Information;
log.EventID = LoggingSeverity.Information;
log.Message = JsonConvert.SerializeObject(ei);
}
catch (Exception genEx)
{
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentFailure,
LoggingSeverity = LoggingSeverity.HighSeverity,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = $"Data failed to be updated within SharePoint - {genEx.Message} - see stacktrace for more information.",
StackTrace = genEx.StackTrace,
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Error;
log.EventID = LoggingSeverity.HighSeverity;
log.Message = JsonConvert.SerializeObject(ei);
AddToLog(log);
return false;
}
AddToLog(log);
return true;
}
else
{
// If a different definition type is passed in throw an appropriate exception.
// This should be caught at runtime only.
throw new TypeLoadException(nameof(SharePointDefinition));
}
}
I checked the code you posted and I see that you are updating the list of objects, in your case listItems, instead of a ListItem in ListItemCollection, in your case listItemCol.
To be more clear, I believe you can try to replace listItems with listItemCol.
For instance, instead of:
listItems[existingItemIndex].ParseAndSetFieldValue(keyValuePair.Key, keyValuePair.Value);
use
listItemCol[existingItemIndex][keyValuePair.Key] = keyValuePair.Value;
So I went away and re-read a lot of documentation and looked over some examples, and I wondered why the exception handler never returned errors even though it's obvious there were issues with the query being executed. If you look at the Microsoft documentation and their example here you'll see that they don't show you how to use exceptionScopeSubmit.HasException component of the exception scope object. Which lead to a really stupid assumption on my part. It turns out that the exception block runs on the SharePoint server and should be used to fix issues you might have caused during an expected exception in your query.
Not only that but wrapping the ExecuteQuery in the try catch is redundant when using an exception scope as it means that method will no longer throw an exception. You actually have to assess exceptionScopeSubmit.HasException after the execution to pull back some more detailing information on errors reported by the SharePoint server side execution of your query.
So now I'm using it as below, and I can get detailed error information without having to do some stupid manual debugging which would easily take me hours to track down silly issues. So in case anyone stumbles across this having the same issues, I hope it helps.
ExceptionHandlingScope exceptionScopeFetch = new ExceptionHandlingScope(clientContext);
// variables.
IntegrationEventLog log = new IntegrationEventLog();
EventInformation ei = new EventInformation();
List list = null;
ListItemCollection listItemCol = null;
using (exceptionScopeFetch.StartScope())
{
using (exceptionScopeFetch.StartTry())
{
// get the required list
list = clientContext.Web.Lists.GetByTitle(definition.ListName);
// create query
listItemCol = list.GetItems(CamlQuery.CreateAllItemsQuery());
// set these items for retrieval.
clientContext.Load(listItemCol);
}
using (exceptionScopeFetch.StartCatch())
{
//
}
using (exceptionScopeFetch.StartFinally())
{
//
}
}
// get item instances first
clientContext.ExecuteQuery();
if (exceptionScopeSubmit.HasException)
{
ei = new EventInformation()
{
LoggingEventType = LoggingEventType.DataSentFailure,
LoggingSeverity = LoggingSeverity.HighSeverity,
SerialisedMessage = JsonConvert.SerializeObject(definition),
ServiceMessage = $"Error at SharePoint exception handler during submit to list: Message: {exceptionScopeFetch.ErrorMessage}",
StackTrace = exceptionScopeFetch.ServerStackTrace,
TimeGenerated = DateTime.Now,
TimeWritten = DateTime.Now,
};
log.EventLogEntryType = EventLogEntryType.Error;
log.EventID = LoggingSeverity.HighSeverity;
log.Message = JsonConvert.SerializeObject(ei);
AddToLog(log);
}

a list of all users: Ldap referral error (LDAPReferralException)

I am using active directory and want a list of all users, basically in dotnet core. But I am receiving an exception:
Search result reference received, and referral following is off
Below is my code.
LdapSearchResults lsc = lc.Search("DC = xyz, DC = local", LdapConnection.SCOPE_ONE , "(|(objectClass = person)(objectClass = user))", null, false);
Necromancing - just in case the links go dark.
To fix it in your application, set ReferralFollowing to true.
if you get the message
Search result reference received, and referral following is off
, add
Novell.Directory.Ldap.LdapSearchConstraints cons = lc.SearchConstraints;
cons.ReferralFollowing = true;
lc.Constraints = cons;
to your code.
Example:
public static void GetUsers()
{
System.Collections.Generic.List<ARSoft.Tools.Net.Dns.SrvRecord> lsLdap = GetLdap();
ARSoft.Tools.Net.Dns.SrvRecord ldap = lsLdap[0];
string[] attrs = new string[] { "cn", "distinguishedName", "sAMAccountName", "userPrincipalName", "displayName", "givenName", "sn", "mail", "mailNickname", "memberOf", "homeDirectory", "msExchUserCulture" };
// CN = Common Name
// OU = Organizational Unit
// DC = Domain Component
string searchBase = "DC=cor,DC=local";
string searchFilter = "(&(objectClass=user)(objectCategory=person))";
string ldapHost = MySamples.TestSettings.ldapHost;
int ldapPort = MySamples.TestSettings.ldapPort;//System.Convert.ToInt32(args[1]);
string loginDN = MySamples.TestSettings.loginDN; // args[2];
string password = MySamples.TestSettings.password; // args[3];
Novell.Directory.Ldap.LdapConnection lc = new Novell.Directory.Ldap.LdapConnection();
int ldapVersion = Novell.Directory.Ldap.LdapConnection.Ldap_V3;
try
{
// connect to the server
lc.Connect(ldap.Target.ToString(), ldap.Port);
// bind to the server
lc.Bind(ldapVersion, loginDN, password);
Novell.Directory.Ldap.LdapSearchConstraints cons = lc.SearchConstraints;
cons.ReferralFollowing = true;
lc.Constraints = cons;
// To enable referral following, use LDAPConstraints.setReferralFollowing passing TRUE to enable referrals, or FALSE(default) to disable referrals.
Novell.Directory.Ldap.LdapSearchResults lsc = lc.Search(searchBase,
Novell.Directory.Ldap.LdapConnection.SCOPE_SUB,
searchFilter,
attrs,
false,
(Novell.Directory.Ldap.LdapSearchConstraints)null);
while (lsc.HasMore())
{
Novell.Directory.Ldap.LdapEntry nextEntry = null;
try
{
nextEntry = lsc.Next();
}
catch (Novell.Directory.Ldap.LdapReferralException eR)
{
// https://stackoverflow.com/questions/46052873/ldap-referal-error
// The response you received means that the directory you are requesting does not contain the data you look for,
// but they are in another directory, and in the response there is the information about the "referral" directory
// on which you need to rebind to "redo" the search.This principle in LDAP are the referral.
// https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/bp31k5d.html
// To enable referral following, use LDAPConstraints.setReferralFollowing passing TRUE to enable referrals, or FALSE (default) to disable referrals.
// are you sure your bind user meaning
// auth.impl.ldap.userid=CN=DotCMSUser,OU=Service Accounts,DC=mycompany,DC=intranet
// auth.impl.ldap.password = mypassword123
// has permissions to the user that is logging in and its groups?
System.Diagnostics.Debug.WriteLine(eR.LdapErrorMessage);
}
catch (Novell.Directory.Ldap.LdapException e)
{
// WARNING: Here catches only LDAP-Exception, no other types...
System.Console.WriteLine("Error: " + e.LdapErrorMessage);
// Exception is thrown, go for next entry
continue;
}
var atCN = nextEntry.getAttribute("cn");
var atUN = nextEntry.getAttribute("sAMAccountName");
var atDN = nextEntry.getAttribute("distinguishedName");
var atDIN = nextEntry.getAttribute("displayName");
if (atCN != null)
System.Console.WriteLine(atCN.StringValue);
if (atUN != null)
System.Console.WriteLine(atUN.StringValue);
if (atDN != null)
System.Console.WriteLine(atDN.StringValue);
if (atDIN != null)
System.Console.WriteLine(atDIN.StringValue);
System.Console.WriteLine("\n" + nextEntry.DN);
Novell.Directory.Ldap.LdapAttributeSet attributeSet = nextEntry.getAttributeSet();
System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
while (ienum.MoveNext())
{
Novell.Directory.Ldap.LdapAttribute attribute = (Novell.Directory.Ldap.LdapAttribute)ienum.Current;
string attributeName = attribute.Name;
string attributeVal = attribute.StringValue;
System.Console.WriteLine(attributeName + "value:" + attributeVal);
}
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
finally
{
// disconnect with the server
lc.Disconnect();
}
}
You have to activate the behaviour which will follow the referral returned by the directory.
The response you received means that the directory you are requesting does not contain the data you look for, but they are in another directory, and in the response there is the information about the "referral" directory on which you need to rebind to "redo" the search. This principle in LDAP are the referral.
I don't know how to do it in C#, but maybe have a look at :
https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/bp31k5d.html

How to get appointment body text for an appointment from outlook exchange server(EWS),c#?

I am trying to fetch email body from exchange server for an appointment for a particular room but did not get a success. Verified all the blogs over internet but nothing was of any help. Here is the code where i am trying to contact exchange server to get the details:
Approach A
service.GetUserAvailability
(
attendees,
new TimeWindow(twStart, twEnd),
AvailabilityData.FreeBusy
).AttendeesAvailability[0].CalendarEvents;
Approach B
public class MailItem
{
public string From;
public string[] Recipients;
public string Subject;
public string Body;
}
public static MailItem[] GetUnreadMailFromInbox(ExchangeService service, string address)
{
// Address is the email address for an meeting room
try
{
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, address);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));
ServiceResponseCollection<GetItemResponse> items =
service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
FindItemsResults<Item> findResults2 = service.FindItems(WellKnownFolderName.Calendar, new ItemView(128));
ServiceResponseCollection<GetItemResponse> items2 =
service.BindToItems(findResults2.Select(item2 => item2.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
return items.Select(item =>
{
return new MailItem()
{
From = address,
Recipients = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(),
Subject = item.Item.Subject,
Body = item.Item.Body.ToString(),
};
}).ToArray();
}
catch (Exception ex)
{
throw ex;
}
}
I need a solution for fetching the body content for appointments from exchange server.
Assuming you are already connecting to the Exchange service successfully and only trying to retrieve appointments body part please try this.
Try Adding EmailMessageSchema.Body to your property set.
service.BindToItems(findResults2.Select(item2 => item2.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.Body, EmailMessageSchema.ToRecipients));

How to retrieve recurring appointments

I am retrieving appoints from an exchange server using the service.FindItems method and it isn't returning recurring appointments. It returns the first instance of recurring items but no more after that and IsRecurring is set to false on the appointment.
This is the code:
private void loadUsersAppointments(string user, int rscID)
{
// Add a search filter that searches on the body or subject.
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsGreaterThan(AppointmentSchema.Start, DateTime.Today.AddDays(-7)));
// Create the search filter.
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
CalendarView V = new CalendarView(DateTime.Today.AddDays(-7), DateTime.Today.AddMonths(1), 1000);
V.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
V.Traversal = ItemTraversal.Shallow;
// Create a view with a page size of 50.
ItemView view = new ItemView(10000);
// Identify the Subject and DateTimeReceived properties to return.
// Indicate that the base property will be the item identifier
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
// Order the search results by the DateTimeReceived in descending order.
view.OrderBy.Add(AppointmentSchema.Start, SortDirection.Descending);
// Set the traversal to shallow. (Shallow is the default option; other options are Associated and SoftDeleted.)
view.Traversal = ItemTraversal.Shallow;
// Send the request to search the Inbox and get the results.
ExchangeService service = GlobalFunc.ElevateGetBinding();
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, user+"#works.local");
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Calendar, searchFilter, view);
List<Item> items = new List<Item>();
foreach (Microsoft.Exchange.WebServices.Data.Appointment appointment in findResults)
{
items.Add(appointment);
}
service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties);
// Process each item.
foreach (Microsoft.Exchange.WebServices.Data.Appointment myItem in items)
{
DevExpress.XtraScheduler.Appointment AddAppt = new DevExpress.XtraScheduler.Appointment();
try {
if (myItem.Subject.StartsWith("Advisor Appointment"))
AddAppt.LabelId = 8;
else
AddAppt.LabelId = 2;
AddAppt.Subject = myItem.Subject;
}
catch { }
try
{
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
try { AddAppt.Start = myItem.Start; }
catch { }
try { AddAppt.Description = myItem.Body; }
catch { }
try { AddAppt.End = myItem.End; }
catch { }
AddAppt.ResourceId = rscID;
schStorage.Appointments.Add(AddAppt);
}
}
Any ideas would be much appreciated.
Thanks
You need to use a CalendarView to get recurring appointments. Instances of recurring appointments are not real items in the Exchange database. Instead, Exchange creates virtual items on the fly when you query for a specific time range.

EWS PullSubscription for New or Modified Calendar Events

I have been searching for an EWS pullsubscription example that allows me to get a list of calendar events that have been created or modified for a user since the pull subscription has been started. I have working code to get this info for the Inbox but I haven't found a good example for how to do this for the Calendar.
Below is an example for the Inbox; can anyone provide me with a link or a code example to accomplish the same thing for Calendar events or appointments using an Exchange Web Service pull subscription?
ExchangeService service;
PullSubscription subscriptionInbox;
private void SetService() {
service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Url = new Uri("https://mail.myserver.com/EWS/Exchange.asmx");
}
private void SetSubscription() {
if(service == null) {
SetService();
}
// Subscribe to pull notifications in the Inbox folder, and get notified when
// a new mail is received, when an item or folder is created, or when an item
// or folder is deleted.
subscriptionInbox = service.SubscribeToPullNotifications(
new FolderId[] { WellKnownFolderName.Inbox },
5 /* timeOut: the subscription will end if the server is not polled within 5 minutes. */,
null /* watermark: null to start a new subscription. */,
EventType.NewMail, EventType.Modified);
}
private void btnGetLatestMessages_Click(object sender, EventArgs e) {
if(subscriptionInbox == null) {
SetSubscription();
}
GetEventsResults eventsInbox = subscriptionInbox.GetEvents();
EmailMessage message;
// Loop through all item-related events.
foreach(ItemEvent itemEvent in eventsInbox.ItemEvents) {
switch(itemEvent.EventType) {
case EventType.NewMail:
try {
Item item = Item.Bind(service, itemEvent.ItemId);
if(item.ItemClass.ToLower() == "IPM.Note".ToLower()) {
message = EmailMessage.Bind(service, itemEvent.ItemId);
MessageBox.Show("Inbox/NewMail - " + message.Subject);
}
} catch(Exception ex) {
MessageBox.Show("EventType.NewMail - " + itemEvent.ItemId);
}
break;
case EventType.Modified:
try {
Item item = Item.Bind(service, itemEvent.ItemId);
if(item.ItemClass.ToLower() == "IPM.Note".ToLower()) {
message = EmailMessage.Bind(service, itemEvent.ItemId);
MessageBox.Show("Inbox/Modified - " + message.Subject);
}
} catch(Exception ex) {
MessageBox.Show("EventType.NewMail - " + itemEvent.ItemId);
}
break;
}
}
}
Simply subscribe to the calendar instead of your inbox with the event types specified.
var subscriptionCalendar = service.SubscribeToPullNotifications(
new[] { new FolderId(WellKnownFolderName.Calendar) },
1440,
null,
EventType.Created, EventType.Modified);
Alternatively, you could create one pull notification for FolderId(WellKnownFolderName.Inbox) and FolderId(WellKnownFolderName.Calendar) with the EventType's you want.

Categories

Resources