Creating Tasks for other users using Exchange Web Services (EWS) Managed API

后端 未结 6 1739
北海茫月
北海茫月 2020-12-08 17:38

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

相关标签:
6条回答
  • 2020-12-08 18:01

    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 <SMTP:humanresources@myDomain.com>")
                    {
                        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 <SMTP:humanresources@mydomain.com>")
         {
             _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "usertwo@mydomain.com");         
         } 
    

    https://msdn.microsoft.com/en-us/library/office/dd633680(v=exchg.80).aspx

    0 讨论(0)
  • 2020-12-08 18:04

    EWS doesn't currently support assigning tasks Check this link

    http://social.msdn.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/2d1d88dc-9b79-4c0c-b438-cc04ff60286f

    0 讨论(0)
  • 2020-12-08 18:06

    Unfortunately, you cant set the Task.DisplayTo property. I would suggest that it's still the case that EWS doesn't support assigning tasks to others (see post) and that, in order to get the functionality you require, you'd have to create the item in the Tasks folder of the user that you want to assign it to (this is different to assigning, which you would do from your own folder)

    While i have this functionality working with the proxy classes, i dont yet have it working with the managed API. I would assume that you can use the FindFolder method to retrieve the assignee's tasks folder, and then create the item there, but i'll have a look, and update when i have a working version.

    Watch this space ;-)

    0 讨论(0)
  • 2020-12-08 18:07

    The code in this post worked for me

    Pasting code for posterity:

    public string CreateTaskItem(string targetMailId)
        {
    
            string itemId = null;
    
            task.Subject = "Amit: sample task created from SDE and EWS";
    
            task.Body = new BodyType();
    
            task.Body.BodyType1 = BodyTypeType.Text;
    
            task.Body.Value = "Amit created task for you!";
    
            task.StartDate = DateTime.Now;
    
            task.StartDateSpecified = true;
    
    
    
            // Create the request to make a new task item.
    
            CreateItemType createItemRequest = new CreateItemType();
    
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
    
            createItemRequest.Items.Items = new ItemType[1];
    
            createItemRequest.Items.Items[0] = task;
    
            /** code from create appointment **/
    
            DistinguishedFolderIdType defTasksFolder = new DistinguishedFolderIdType();
    
            defTasksFolder.Id = DistinguishedFolderIdNameType.tasks;
            defTasksFolder.Mailbox = new EmailAddressType();
    
            defTasksFolder.Mailbox.EmailAddress = targetMailId;
    
            TargetFolderIdType target = new TargetFolderIdType();
    
            target.Item = defTasksFolder;
    
    
    
            createItemRequest.SavedItemFolderId = target;
    
    
            try
    
            {
    
                // Send the request and get the response.
    
                CreateItemResponseType createItemResponse = _esb.CreateItem(createItemRequest);
    
    
    
                // Get the response messages.
    
                ResponseMessageType[] rmta = createItemResponse.ResponseMessages.Items;
    
    
    
                foreach (ResponseMessageType rmt in rmta)
    
                {
    
                    ArrayOfRealItemsType itemArray = ((ItemInfoResponseMessageType)rmt).Items;
    
                    ItemType[] items = itemArray.Items;
    
    
                    // Get the item identifier and change key for each item.
    
                    foreach (ItemType item in items)
    
                    {
    
    
    //the task id
    
                       Console.WriteLine("Item identifier: " + item.ItemId.Id);
    
    
    //the change key for that task, would be used if you want to track changes ...
                        Console.WriteLine("Item change key: " + item.ItemId.ChangeKey);
    
                    }
    
                }
    
            }
    
            catch (Exception e)
    
            {
    
                Console.WriteLine("Error Message: " + e.Message);
    
            }
    
            return itemId;
    
        }
    
    0 讨论(0)
  • 2020-12-08 18:14

    Another option is set use the ExchangeService ImpersonatedUserId property to impersonate the user who will be assigned the Task. Impersonate the user before creating the task and it should be created in their Tasks folder.

    0 讨论(0)
  • 2020-12-08 18:16

    I've been taking a look into this, and i'm not sure it's possible using the Managed API.

    I've got a system set up using four sample user folders, and a central admin user with delegated access to each of those user's mailboxes. When i attempt to find folders using the API, i can only find the folders of the user who's credentials i supply when creating the service object.

    I'm also using the auto-generated proxy objects (only picked up the API to try and help), and I use the following process to create a task for another user (this works correctly...):

    1. Connect to the server as the central admin account.
    2. Create the task object as you would for your own account.
    3. Create a reference to the Tasks folder of the user that you want to send the item to.
    4. Create a CreateItemRequest object to pass to the server, and add the two items from steps 2 and 3 to the request

    When the request is sent, the item is created in the target user's folder.

    I was hoping that this sequence might be possible in the managed API, but it doesnt seem to work.

    I'll keep working on it as i get the chance, but i have other issues with appointments that i'm working on as well. I figured the sequence might help anyone else looking, in case they have more luck.

    Sorry i cant provide any more info at the moment

    0 讨论(0)
提交回复
热议问题