private async void DiscoverResourcesAsync()
        {
            _discoverResourcesCommand.SetCanExecute(false);
            try
            {
                var serverResources = await _serverSettingsViewModel.GetServerResources();

                var calendars    = serverResources.Calendars.Select(c => new CalendarDataViewModel(c)).ToArray();
                var addressBooks = serverResources.AddressBooks.Select(a => new AddressBookDataViewModel(a)).ToArray();
                var taskLists    = serverResources.TaskLists.Select(d => new TaskListDataViewModel(d)).ToArray();

                using (var selectResourcesForm = SelectResourceForm.CreateForFolderAssignment(_optionTasks, ConnectionTests.ResourceType.Calendar, calendars, addressBooks, taskLists))
                {
                    if (selectResourcesForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        var optionList = new List <OptionsModel>();

                        foreach (var resource in calendars.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptionsWithCategory(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            _optionTasks.ValidateBulkProfile(options, resource.Model.Privileges, resource.Model.OwnerProperties);
                            optionList.Add(options);
                        }

                        foreach (var resource in addressBooks.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptions(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            _optionTasks.ValidateBulkProfile(options, resource.Model.Privileges, null);
                            optionList.Add(options);
                        }

                        foreach (var resource in taskLists.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptions(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            optionList.Add(options);
                        }

                        _parent.RequestAdd(optionList);
                        _parent.RequestRemoval(this);
                    }
                }
            }
            catch (Exception x)
            {
                s_logger.Error("Exception while DiscoverResourcesAsync.", x);
                string message = null;
                for (Exception ex = x; ex != null; ex = ex.InnerException)
                {
                    message += ex.Message + Environment.NewLine;
                }
                MessageBox.Show(message, OptionTasks.ConnectionTestCaption);
            }
            finally
            {
                _discoverResourcesCommand.SetCanExecute(true);
            }
        }
        private async void DiscoverResourcesAsync()
        {
            _discoverResourcesCommand.SetCanExecute(false);
            try
            {
                var serverResources = await _serverSettingsViewModel.GetServerResources();

                var calendars    = serverResources.Calendars.Select(c => new CalendarDataViewModel(c)).ToArray();
                var addressBooks = serverResources.AddressBooks.Select(a => new AddressBookDataViewModel(a)).ToArray();
                var taskLists    = serverResources.TaskLists.Select(d => new TaskListDataViewModel(d)).ToArray();
                if (OnlyAddNewUrls)
                {
                    // Exclude all resourcres that have already been configured
                    var options        = (_parent as OptionsCollectionViewModel).Options;
                    var configuredUrls = new HashSet <String>(options.Select(o => o.Model.CalenderUrl));
                    calendars    = calendars.Where(c => !configuredUrls.Contains(c.Uri.ToString())).ToArray();
                    addressBooks = addressBooks.Where(c => !configuredUrls.Contains(c.Uri.ToString())).ToArray();
                    taskLists    = taskLists.Where(c => !configuredUrls.Contains(c.Id)).ToArray();
                }

                // --- Create folders if requested and required
                if (AutoCreateOutlookFolders)
                {
                    // https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-create-a-custom-calendar
                    // https://msdn.microsoft.com/en-us/library/office/ff184655.aspx
                    // Get Outlook's default calendar folder (this is where we create the Kolab folders)
                    GenericComObjectWrapper <Folder> defaultCalendarFolder = new GenericComObjectWrapper <Folder>(Globals.ThisAddIn.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar) as Folder);
                    // Find all Kolab calendars that are not yet synced to an outlook folder
                    foreach (var resource in calendars.Where(c => c.SelectedFolder == null))
                    {
                        string newCalendarName = resource.Name + " (" + Name + ")";
                        GenericComObjectWrapper <Folder> newCalendarFolder = null;
                        try
                        {
                            // Use existing folder if it does exist
                            newCalendarFolder = new GenericComObjectWrapper <Folder>(defaultCalendarFolder.Inner.Folders[newCalendarName] as Folder);
                        }
                        catch
                        {
                            // Create missing folder
                            newCalendarFolder = new GenericComObjectWrapper <Folder>(defaultCalendarFolder.Inner.Folders.Add(newCalendarName, OlDefaultFolders.olFolderCalendar) as Folder);
                            // Make sure it has not been renamed to "name (this computer only)"
                            newCalendarFolder.Inner.Name = newCalendarName;
                        }

                        // use the selected folder for syncing with kolab
                        resource.SelectedFolder = new OutlookFolderDescriptor(newCalendarFolder.Inner.EntryID, newCalendarFolder.Inner.StoreID, newCalendarFolder.Inner.DefaultItemType, newCalendarFolder.Inner.Name, 0);
                    }

                    // Create and assign all Kolab address books that are not yet synced to an outlook folder
                    GenericComObjectWrapper <Folder> defaultAddressBookFolder = new GenericComObjectWrapper <Folder>(Globals.ThisAddIn.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts) as Folder);
                    foreach (var resource in addressBooks.Where(c => c.SelectedFolder == null))
                    {
                        string newAddressBookName = resource.Name + " (" + Name + ")";
                        GenericComObjectWrapper <Folder> newAddressBookFolder = null;
                        try
                        {
                            newAddressBookFolder = new GenericComObjectWrapper <Folder>(defaultAddressBookFolder.Inner.Folders[newAddressBookName] as Folder);
                        }
                        catch
                        {
                            newAddressBookFolder            = new GenericComObjectWrapper <Folder>(defaultAddressBookFolder.Inner.Folders.Add(newAddressBookName, OlDefaultFolders.olFolderContacts) as Folder);
                            newAddressBookFolder.Inner.Name = newAddressBookName;
                        }

                        resource.SelectedFolder = new OutlookFolderDescriptor(newAddressBookFolder.Inner.EntryID, newAddressBookFolder.Inner.StoreID, newAddressBookFolder.Inner.DefaultItemType, newAddressBookFolder.Inner.Name, 0);
                    }

                    // Create and assign all Kolab address books that are not yet synced to an outlook folder
                    GenericComObjectWrapper <Folder> defaultTaskListsFolder = new GenericComObjectWrapper <Folder>(Globals.ThisAddIn.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderTasks) as Folder);
                    foreach (var resource in taskLists.Where(c => c.SelectedFolder == null))
                    {
                        string newTaskListName = resource.Name + " (" + Name + ")";
                        GenericComObjectWrapper <Folder> newTaskListFolder = null;
                        try
                        {
                            newTaskListFolder = new GenericComObjectWrapper <Folder>(defaultTaskListsFolder.Inner.Folders[newTaskListName] as Folder);
                        }
                        catch
                        {
                            newTaskListFolder            = new GenericComObjectWrapper <Folder>(defaultTaskListsFolder.Inner.Folders.Add(newTaskListName, OlDefaultFolders.olFolderTasks) as Folder);
                            newTaskListFolder.Inner.Name = newTaskListName;
                        }

                        resource.SelectedFolder = new OutlookFolderDescriptor(newTaskListFolder.Inner.EntryID, newTaskListFolder.Inner.StoreID, newTaskListFolder.Inner.DefaultItemType, newTaskListFolder.Inner.Name, 0);
                    }
                }

                using (var selectResourcesForm = SelectResourceForm.CreateForFolderAssignment(_optionTasks, ConnectionTests.ResourceType.Calendar, calendars, addressBooks, taskLists))
                {
                    if (selectResourcesForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        var optionList = new List <OptionsModel>();

                        foreach (var resource in calendars.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptions(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            _optionTasks.ValidateBulkProfile(options, resource.Model.Privileges, resource.Model.OwnerProperties);
                            optionList.Add(options);
                        }

                        foreach (var resource in addressBooks.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptions(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            _optionTasks.ValidateBulkProfile(options, resource.Model.Privileges, null);
                            optionList.Add(options);
                        }

                        foreach (var resource in taskLists.Where(c => c.SelectedFolder != null))
                        {
                            var options = CreateOptions(resource);
                            _serverSettingsViewModel.SetResourceUrl(options, resource.Model);
                            optionList.Add(options);
                        }

                        _parent.RequestAdd(optionList);
                        _parent.RequestRemoval(this);
                    }
                }
            }
            catch (Exception x)
            {
                s_logger.Error("Exception while DiscoverResourcesAsync.", x);
                string message = null;
                for (Exception ex = x; ex != null; ex = ex.InnerException)
                {
                    message += ex.Message + Environment.NewLine;
                }
                MessageBox.Show(message, OptionTasks.ConnectionTestCaption);
            }
            finally
            {
                _discoverResourcesCommand.SetCanExecute(true);
            }
        }