EWS API从Outlook日历中删除约会

时间:2020-06-04 10:42:29

标签: c# outlook exchangewebservices

你好, 你们是否知道EWS C#API是否已更改?我有一个自动批处理应用程序,通常可以正常运行,但是在过去的1-2周中,它无法删除标记的日历并进行更新。如果不是,是否有人可以指出我可以找到补丁版本/说明的地方,以查看是否有任何更改?

 class Program {
    private const int ParallelCount = 3;
    private static string[] ErrorRepportRecipients = new string[] { "xx", "xx" };
    [DllImport("user32.dll")]
    static extern void FlashWindow(IntPtr a, bool b);
    private static ExtendedPropertyDefinition SafeAppointmentFlag = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "SAFE_APPOINTMENT_FLAG", MapiPropertyType.Boolean);
    static void Main(string[] args) {
        Console.Title = "SAFE til Exchange synkronisering";
        Console.BufferHeight = 1000;
        Console.BufferWidth = 120;
        Console.SetWindowSize(120, 30);
        Console.WriteLine("Starter Exchange kalender synkronisering");
        if(SystemVars.IsProductionEnvironment()) {
            AppDomain.CurrentDomain.UnhandledException += Program.CurrentDomain_UnhandledException;
           // ApplicationLog.LogEvent(ApplicationLogEntry.CreateLogEntryForAgent("Application started", LogTypes.Notification));
            NDBusiness.Audit.Environment.CheckApplicationState();
        }
        Console.WriteLine();
        Console.WriteLine("Forbinder til Exchange... (" + ParallelCount + " forbindelser)");
        ConcurrentQueue<ExchangeService> serviceQueue = new ConcurrentQueue<ExchangeService>();
        Parallel.For(0, ParallelCount, i => {
            serviceQueue.Enqueue(EWS.GetService("xx@xx.dk", new WebCredentials("xx@xx.dk", "xx")));
            Console.WriteLine("Forbindelse " + (i + 1).ToString() + " klar");
        });
        var syncronizer = new SafeScheduleSyncronizer();
        #region Delete
        if(args.Contains("delete")) {
            int attemptedDeleteCount = 0;
            int deletedCount = 0;
            Console.WriteLine();
            Console.WriteLine("Kører rydning af markerede kalendere");
          //  if(SystemVars.IsProductionEnvironment())
           //     ApplicationLog.LogEvent(ApplicationLogEntry.CreateLogEntryForAgent("Calendar delete started", LogTypes.BatchRun));
            ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();
            List<string> permissionEx = new List<string>();
            List<string> noGuidEx = new List<string>();
            using(var conn = new SCDriftConnection()) {
                var agentsMarkedForCalendarClear = SAFEAgent.GetAgents(conn, new QueryParameters<SAFEAgent, SAFEAgent.ExchangeSettings>((a, e) => e.DeleteAll == true));
                try {
                    Parallel.ForEach(agentsMarkedForCalendarClear,
                                     new ParallelOptions() { MaxDegreeOfParallelism = ParallelCount },
                                     a => {
                                         ExchangeService service;
                                         var dequeued = serviceQueue.TryDequeue(out service);
                                         if(dequeued)
                                             try {
                                                 Interlocked.Increment(ref attemptedDeleteCount);
                                                 using(var scDriftConn = new SCDriftConnection()) {
                                                     try {
                                                         Console.WriteLine("Rydder kalender for " + a.Initials);
                                                         //  syncronizer.DeleteAllSafeApointments(scDriftConn, service, a, new ClosedRange<DateTime>(DateTime.Today.AddDays(-7), DateTime.Today.AddDays(14)));
                                                         syncronizer.DeleteAllSafeApointments(scDriftConn, service, a);
                                                         Console.WriteLine("Kalender ryddet for " + a.Initials);
                                                         Interlocked.Increment(ref deletedCount);
                                                     } catch(Exception ex) {
                                                         if(ex.InnerException != null) {
                                                             if(ex.InnerException is InvalidOperationException) {
                                                                 if(ex.InnerException.Message == "Agent has not assigned editor permissions") {
                                                                     a.ExchangeSyncronizationSettings.HasCalendarEditorPermission = false;
                                                                     a.ExchangeSyncronizationSettings.Save(conn);
                                                                     Console.WriteLine("Rettigheder til kalender mangler for " + a.Initials);
                                                                     permissionEx.Add(a.Initials);
                                                                 } else
                                                                     exceptions.Add(ex.InnerException);
                                                             } else if(ex.InnerException is ServiceResponseException) {
                                                                 if (ex.InnerException.Message.ValueIn("The SMTP address has no mailbox associated with it.", "The specified folder could not be found in the store."))
                                                                 {
                                                                     a.ExchangeSyncronizationSettings.HasCalendarEditorPermission = false;
                                                                     a.ExchangeSyncronizationSettings.Save(conn);
                                                                     Console.WriteLine("Rettigheder til kalender mangler for " + a.Initials);
                                                                     permissionEx.Add(a.Initials);
                                                                 }
                                                                 else
                                                                 {
                                                                     exceptions.Add(ex.InnerException);
                                                                     if (ex.InnerException.Message.ToUpper().StartsWith("NO MAILBOX WITH SUCH GUID"))
                                                                     {
                                                                         noGuidEx.Add(a.Initials);
                                                                     }
                                                                 }

                                                             } else if(ex.InnerException is OperationCanceledException)
                                                                 throw;
                                                         } else
                                                             exceptions.Add(new Exception("Error during calendar delete of " + a.Initials + ".", ex));
                                                     }
                                                     a.ExchangeSyncronizationSettings.DeleteAll = false;
                                                     a.ExchangeSyncronizationSettings.Save(scDriftConn);
                                                 }
                                             } finally {
                                                 serviceQueue.Enqueue(service);
                                             }
                                         else
                                             throw new InvalidOperationException("Could not dequeue service.");
                                     });
                } catch(AggregateException ex) {
                    throw ex.InnerExceptions.First();
                }
            }
            if(exceptions.Count > 0) {
                Console.WriteLine("Der opstod fejl ved " + exceptions.Count + " opdateringer");
            }

            Console.WriteLine("Rydning af markerede kalendere færdig (" + deletedCount + "/" + attemptedDeleteCount + " ryddet)");
        }
        #endregion

0 个答案:

没有答案