As an \"EWS Managed API Newbie\", I\'m having some problems finding examples and documentation about creating and managing Tasks.
I\'ve managed to create a task for
Ive been looking into this more recently and have the following:
I am running a Console application which will set up a streaming connection to check for new emails arriving in the mailbox for userOne@myDomain.com
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
WebCredentials wbcred = new WebCredentials("userone", "password", "mydomain");
service.Credentials = wbcred;
Console.WriteLine("Attempting to autodiscover Url...");
service.AutodiscoverUrl("userone@mydomain.com", RedirectionUrlValidationCallback);
EWSConnection.SetStreamingNotifications(service);
Console.ReadKey();
Environment.Exit(0);
}
My EWSConnection
static class looks loosely like this:
public static void SetStreamingNotifications(ExchangeService service)
{
_service = service;
try
{ var subscription = service.SubscribeToStreamingNotifications(
new FolderId[] { WellKnownFolderName.Inbox },
EventType.NewMail);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 5);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.Open();
_subscription = subscription;
_subscriptionConnection = connection;
Console.WriteLine($"Connection Open:{connection.IsOpen}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
With this, you can see I have subscribed to the OnNotificationEvent
and in turn my OnEvent
method will be invoked when a new email arrives. When a new email arrives to this mailbox I have a requirement to create a task and assign it to the relevant person, based on what the ToAddress
property is.
private static void CreateTask(NotificationEvent notification, RecievedMail recievedMail)
{
try
{
Console.WriteLine("Attempting to create task");
Microsoft.Exchange.WebServices.Data.Task task = new Microsoft.Exchange.WebServices.Data.Task(_service);
task.DueDate = DateTime.Now.AddDays(7);
task.Body = recievedMail.Body;
task.Subject = recievedMail.Subject;
string targetSharedEmailAddress = null;
if (recievedMail.ToAddress.ToString() == "humanresources ")
{
targetSharedEmailAddress = "usertwo@mydomain.com";
}
task.Save(new FolderId(WellKnownFolderName.Tasks,targetSharedEmailAddress)); //
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
At first you can see that I tried adding the person that I wanted the task to be created for in the task.Save
method, however once I went to Outlook to interact with the newly created task, the owner was still userone
, i.e. the person who's credentials were used to connect to the service, this was an issue for me as I need the task owner to be usertwo
.
This was accomplished by dropping the targetSharedEmailAddress
variable and instead using the ImpersonatedUserId
property of the ExchangeServer
object.
if (recievedMail.ToAddress.ToString() == "humanresources ")
{
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "usertwo@mydomain.com");
}
https://msdn.microsoft.com/en-us/library/office/dd633680(v=exchg.80).aspx