locked
Updating Inbox Rules using EWS or Set-InboxRule RRS feed

  • Question

  • I have written some code (below) to update an Inbox rule following the example from here http://msdn.microsoft.com/en-us/library/ff597936(v=exchg.80).aspx The result is that the code does find the rule correctly, but when it attempts to update, I get the following error: "A validation error occurred while executing the rule operation. Exception of type Microsoft.Exchange.WebServices.Data.UpdateInboxRulesException' was thrown." I feel that my accounts creds are correct and I can't figure what I'm doing wrong.

    An alternate method for updating rules might be:

    Set-InboxRule -Mailbox user -Identity "TESTRULE1" -MoveToFolder "user:\folder" -AlwaysDeleteOutlookRulesBlob

    but I want to insert a folder that exists under the Archive mailbox here, so I don't know how to insert the proper path. It does not seem to accept a MailboxFolderIDParameter directly and wants a path as specified above.

    Any help on either method would be greatly appreciated!

    Regard,
    Jeff

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Exchange.WebServices;
    using Microsoft.Exchange.WebServices.Data;
    using Microsoft.Exchange.WebServices.Autodiscover;
    
    namespace UpdateInboxRules
    {
        class Program
        {
            static void Main(string[] args)
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
                service.Credentials = new WebCredentials("resourceuser1@company.com", "password");
                service.AutodiscoverUrl("user1@company.com");
    
                RuleCollection ruleCollection = service.GetInboxRules("user1@company.com");
                foreach (Rule ruleinCollection in ruleCollection)
                {
                    if (ruleinCollection.DisplayName == "TESTRULE1")
                    {
                        ruleinCollection.Conditions.ContainsSubjectStrings.Clear();
                        ruleinCollection.Conditions.ContainsSubjectStrings.Add("This is Junk");
    
                        SetRuleOperation setRuleOperation = new SetRuleOperation(ruleinCollection);
                        service.UpdateInboxRules(new RuleOperation[] { setRuleOperation }, true);
                    }
                }
    
            }
        }
    }

    Tuesday, July 3, 2012 9:10 PM

Answers

  • With Inbox Rules Move and Copy rules can only Move or Copy an Item to a folder within the users Mailbox. The archive is essentially another Mailbox so its not a valid target for a Rule. If you want things to be moved into the Archive you can create a folder in the Mailbox create the rule to move items into that folder. Then apply a retention policy to that folder to move the Item into the Archive. see http://technet.microsoft.com/en-us/library/hh529934.aspx.

    With the error your getting did you create the original rule using EWS ? because of the differences in the way EWS and Outlook implement rules you should only really update rules created in EWS with EWS (or the EMS). You can check the IsNotSupported property on a rule to see if you can update it.

    Cheers
    Glen

    • Marked as answer by JeffGirotto Thursday, July 5, 2012 12:59 PM
    Wednesday, July 4, 2012 6:49 AM

All replies

  • With Inbox Rules Move and Copy rules can only Move or Copy an Item to a folder within the users Mailbox. The archive is essentially another Mailbox so its not a valid target for a Rule. If you want things to be moved into the Archive you can create a folder in the Mailbox create the rule to move items into that folder. Then apply a retention policy to that folder to move the Item into the Archive. see http://technet.microsoft.com/en-us/library/hh529934.aspx.

    With the error your getting did you create the original rule using EWS ? because of the differences in the way EWS and Outlook implement rules you should only really update rules created in EWS with EWS (or the EMS). You can check the IsNotSupported property on a rule to see if you can update it.

    Cheers
    Glen

    • Marked as answer by JeffGirotto Thursday, July 5, 2012 12:59 PM
    Wednesday, July 4, 2012 6:49 AM
  • Thanks, Glen.

    I did some testing and found that Move and Copy can move items to the archive, but it will make the rule client-only and when pulling information about the rule, the move/copy field shows blank, so the server doesn't contain the info about the rule.

    Many thanks, Glen. You have saved me from spending more time on this.

    Regards,
    Jeff

    Thursday, July 5, 2012 12:59 PM