public static MasterCategoryList Bind(ExchangeService service, string smtpAddress)
        {
            try
            {
                FolderId TargetFolder = new FolderId(WellKnownFolderName.Calendar, smtpAddress);
                var      item         = UserConfiguration.Bind(service, "CategoryList", TargetFolder, UserConfigurationProperties.XmlData);

                var reader     = new StreamReader(new MemoryStream(item.XmlData), Encoding.UTF8, true);
                var serializer = new XmlSerializer(typeof(MasterCategoryList));
                var result     = (MasterCategoryList)serializer.Deserialize(reader);
                result._UserConfigurationItem = item;
                return(result);
            }
            catch (System.ArgumentNullException)
            {
                // There is a folder but it didn't have any categories
                // Create an empty list and return it
                FolderId TargetFolder = new FolderId(WellKnownFolderName.Calendar, smtpAddress);
                var      item         = UserConfiguration.Bind(service, "CategoryList", TargetFolder,
                                                               UserConfigurationProperties.XmlData);
                var result = new MasterCategoryList();
                result.Categories             = new List <Category>();
                result._UserConfigurationItem = item;
                return(result);
            }
            catch (Exception ex)
            {
                // Seems there is no MasterCategoryList, we will try to create a new one
                log.WriteErrorLog(ex.ToString());
                log.WriteErrorLog(ex.Message);
                return(CreateNewMasterCartegories(service));
            }
        }
        /// <summary>
        /// Export categories from the target mailbox into a file
        /// </summary>
        /// <param name="Service">EWS service</param>
        /// <param name="FileName">Full path of the file name</param>
        /// <param name="TargetAddress">SMTP address from the target mailbox</param>
        /// <returns>Count of exported categories</returns>
        public static int Export(ExchangeService Service, string FileName, string TargetAddress)
        {
            try
            {
                var CategoryList = MasterCategoryList.Bind(Service, TargetAddress);
                // Note: if you connect to a mailbox without an CategoryList you will not get an exception for the first time, after EWS throws an System.ArgumentNullException

                if (CategoryList != null)
                {
                    if (CategoryList.Categories.Count > 0)
                    {
                        try
                        {
                            if (FileName != "")
                            {
                                log.WriteInfoLog(string.Format("Saving categories to file: {0}", FileName));

                                FileStream    file   = System.IO.File.Create(FileName);
                                XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(MasterCategoryList));

                                writer.Serialize(file, CategoryList);
                                file.Close();
                                log.WriteInfoLog(string.Format("{0} Categories successfully saved to file: {1}", CategoryList.Categories.Count, FileName));
                                return(CategoryList.Categories.Count);
                            }
                        }
                        catch
                        {
                            log.WriteErrorLog("Error on saving file. Check permissions.");
                            return(0);
                        }
                    }
                    else
                    {
                        log.WriteInfoLog("No categories in mailbox to export.");
                        return(0);
                    }
                }
                else
                {
                    log.WriteErrorLog("Export failed.");
                    return(0);
                }
                return(0);
            }
            catch (System.ArgumentNullException)
            {
                log.WriteErrorLog("No categories in mailbox to export.");
                throw new System.ArgumentNullException();
            }
            catch (Exception ex)
            {
                log.WriteErrorLog(ex.ToString());
                log.WriteErrorLog(ex.Message);
                return(0);
            }
        }
        /// <summary>
        /// Copy categores from a source mailbox into the target mailbox
        /// </summary>
        /// <param name="SourceService">EWS service (source mailbox)</param>
        /// <param name="TargetService">EWS service (target mailbox)</param>
        /// <param name="ClearOnImport">>If set all categories in the target mailbox will be deleted before import</param>
        /// <param name="SourceAddress">SMTP address of the source mailbox</param>
        /// <param name="TargetAddress">SMTP address of the target mailbox</param>
        /// <returns>Count of copied categories</returns>
        public static int CopyCategories(ExchangeService SourceService, ExchangeService TargetService, bool ClearOnImport, string SourceAddress, string TargetAddress)
        {
            if (SourceService != null && TargetService != null)
            {
                log.WriteInfoLog("Loading source and target categories.");
                // Loading source and target
                var sourceCategoryList = MasterCategoryList.Bind(SourceService, SourceAddress);
                var targetCategoryList = MasterCategoryList.Bind(TargetService, TargetAddress);

                if (ClearOnImport)
                {
                    if (targetCategoryList.Categories.Count > 0)
                    {
                        for (int i = targetCategoryList.Categories.Count - 1; i >= 0; i--)
                        {
                            targetCategoryList.Categories.RemoveAt(i);
                        }

                        // update cleared category targetCategoryList
                        targetCategoryList.Update();
                    }
                }
                try
                {
                    // Get all names from the target categories
                    List <string> targetNames = new List <string>();
                    foreach (Category tempCategory in targetCategoryList.Categories)
                    {
                        targetNames.Add(tempCategory.Name);
                    }

                    int importedCategories = 0;
                    for (int i = 0; i < sourceCategoryList.Categories.Count; i++)
                    {
                        if (!(targetNames.Contains(sourceCategoryList.Categories[i].Name)))
                        {
                            targetCategoryList.Categories.Add(sourceCategoryList.Categories[i]);
                            importedCategories++;
                        }
                    }

                    targetCategoryList.Update();
                    return(importedCategories);
                }
                catch
                {
                    log.WriteErrorLog("Error on copying the categories.");
                    return(0);
                }
            }
            return(0);
        }
 public static MasterCategoryList CreateNewMasterCartegories(ExchangeService service)
 {
     try
     {
         log.WriteDebugLog("No Master category list found. Try to create a new one.");
         var result = new MasterCategoryList();
         result.Categories             = new List <Category>();
         result._UserConfigurationItem = new UserConfiguration(service);
         result._UserConfigurationItem.Save("CategoryList", WellKnownFolderName.Calendar);
         log.WriteDebugLog("Master category list created successfully.");
         return(result);
     }
     catch (Exception ex)
     {
         log.WriteErrorLog("No Master category list found. Creation failed. It's not possible to Ex- or Import categories for this mailbox");
         log.WriteErrorLog(ex.ToString());
         log.WriteErrorLog(ex.Message);
         return(null);
     }
 }
        /// <summary>
        /// Import categories from a file into the MasterCategoryList in the target mailbox
        /// </summary>
        /// <param name="Service">EWS service</param>
        /// <param name="FileName">Full path of the file name</param>
        /// <param name="ClearOnImport">If set all categories is the target mailbox will be deleted before import</param>
        /// <param name="TargetAddress">SMTP address from the target mailbox</param>
        /// <returns>Count of imported categories</returns>
        public static int Import(ExchangeService Service, string FileName, bool ClearOnImport, string TargetAddress)
        {
            var CategoryList = new MasterCategoryList();

            log.WriteInfoLog(string.Format("Loading XML file: {0}", FileName));

            try
            {
                TextReader    reader        = new StreamReader(FileName);
                XmlSerializer newSerializer = new XmlSerializer(typeof(MasterCategoryList));

                CategoryList = (MasterCategoryList)newSerializer.Deserialize(reader);
            }
            catch
            {
                log.WriteErrorLog("Error on reading file.");
            }

            try
            {
                log.WriteInfoLog("Importing categories to mailbox");

                var targetCategoryList = MasterCategoryList.Bind(Service, TargetAddress);
                if (ClearOnImport)
                {
                    if (targetCategoryList.Categories.Count > 0)
                    {
                        for (int i = targetCategoryList.Categories.Count - 1; i >= 0; i--)
                        {
                            targetCategoryList.Categories.RemoveAt(i);
                        }

                        // update cleared category targetCategoryList
                        targetCategoryList.Update();
                    }
                }

                // if there is nothing no need to compare
                if (targetCategoryList.Categories.Count == 0)
                {
                    targetCategoryList.Categories = CategoryList.Categories;
                    targetCategoryList.Update();
                    log.WriteInfoLog("categories successfully imported");
                    return(CategoryList.Categories.Count);
                }
                else
                {
                    // Get all names from the target categories
                    List <string> targetNames = new List <string>();
                    foreach (Category tempCategory in targetCategoryList.Categories)
                    {
                        targetNames.Add(tempCategory.Name);
                    }

                    int importedCategories = 0;
                    for (int i = 0; i < CategoryList.Categories.Count; i++)
                    {
                        if (!(targetNames.Contains(CategoryList.Categories[i].Name)))
                        {
                            targetCategoryList.Categories.Add(CategoryList.Categories[i]);
                            importedCategories++;
                        }
                    }
                    targetCategoryList.Update();
                    log.WriteInfoLog(string.Format("{0} Categories successfully imported", importedCategories));
                    return(importedCategories);
                }
            }
            catch
            {
                log.WriteErrorLog("Error on importing categories. Check XML file and permissions to the mailbox.");
                return(0);
            }
        }