OfficeDev / ews-managed-api

Other
584 stars 320 forks source link

Incorrect priority for InboxRule when created sequentially #14

Open LukeMarlin opened 9 years ago

LukeMarlin commented 9 years ago

There is a problem creating InboxRules sequentially while keeping the order

For example creating 9 rules (with InboxRule.Priority set from 1 to 9) one after one will give the following order on the account : 2,3,4,5,6,7,8,9,1. It seems that creating them simultaneously doesn't cause this issue.

There is also a problem when I try to save new InboxRules between existing ones. If I keep the order 2,3,4,5,6,7,8,1 and try to insert "a" at priority 2 and "b" at priority 8, here is the order I get : 2,a,3,4,5,6,7,8,b,1.

Although "a" is at his place, "b" is at the 9th place instead of 8th.

Here is a quickly done program that allows to test this behaviour. Just set a service URL, account, password, uncomment one of the three methods and launch it. Before pressing enter in the console, check the result on Outlook Web Application.

class Program
{
    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
        service.Url = new Uri("yourservice");
        service.Credentials =
            new NetworkCredential("your@ema.il", "pwd");

        //CreateSequentially(service);
        //CreateSequentiallyThenAddInBetween(service);
        //CreateSimultaneously(service);

        Console.WriteLine(@"/!\Rules have been migrated, press enter to remove them/!\");
        Console.ReadLine();
        ClearRules(service);

    }

    /// <summary>
    /// Creates rules one by one with an incrementing priority
    /// Then add "a" at priority 2 and "b" at priority 8
    /// </summary>
    /// <param name="service">Target Exchange service</param>
    private static void CreateSequentiallyThenAddInBetween(ExchangeService service)
    {
        CreateSequentially(service);
        Rule r = new Rule();
        r.Conditions.ContainsBodyStrings.Add("^$ù$^*ù^**ù");
        r.Actions.Delete = true;
        r.DisplayName = "a";
        r.Priority = 2;
        CreateRuleOperation op = new CreateRuleOperation(r);
        service.UpdateInboxRules(new CreateRuleOperation[] { op }, true);

        r = new Rule();
        r.Conditions.ContainsBodyStrings.Add("^$ù$^*ù^**ù");
        r.Actions.Delete = true;
        r.DisplayName = "b";
        r.Priority = 8;
        op = new CreateRuleOperation(r);
        service.UpdateInboxRules(new CreateRuleOperation[] { op }, true);
    }

    /// <summary>
    /// Creates rules one by one with an incrementing priority
    /// </summary>
    /// <param name="service">Target Exchange service</param>
    private static void CreateSequentially(ExchangeService service)
    {

        for (int i = 1; i < 9; i++)
        {
            Rule r = new Rule();
            r.Conditions.ContainsBodyStrings.Add("^$ù$^*ù^**ù");
            r.Actions.Delete = true;
            r.DisplayName = i.ToString();
            r.Priority = i;
            CreateRuleOperation op = new CreateRuleOperation(r);
            service.UpdateInboxRules(new CreateRuleOperation[] { op }, true);
        }

    }

    /// <summary>
    /// Creates rules in one call
    /// </summary>
    /// <param name="service">Target Exchange service</param>
    private static void CreateSimultaneously(ExchangeService service)
    {
        var operations = new List<CreateRuleOperation>();

        for (int i = 1; i < 9; i++)
        {
            Rule r = new Rule();
            r.Conditions.ContainsBodyStrings.Add("^$ù$^*ù^**ù");
            r.Actions.Delete = true;
            r.DisplayName = i.ToString();
            r.Priority = i;
            CreateRuleOperation op = new CreateRuleOperation(r);
            operations.Add(op);
        }
        service.UpdateInboxRules(operations, true);
    }

    /// <summary>
    /// Clear all inbox rules
    /// </summary>
    /// <param name="service">Target Exchange service</param>
    private static void ClearRules(ExchangeService service)
    {
        var destRules = service.GetInboxRules();
        var operations = new List<DeleteRuleOperation>();

        foreach (var item in destRules)
        {
            DeleteRuleOperation op = new DeleteRuleOperation(item.Id);
            operations.Add(op);
        }

        service.UpdateInboxRules(operations, true);
    }
}
LukeMarlin commented 9 years ago

Any news about this case ? We took a look at this again and we noticed the same behaviors.