private Folder CreateNewFolder(DefaultFolderContext context, string displayName, StoreObjectId parentFolderObjectId) { Folder result; if (this.storeObjectType == StoreObjectType.Folder || this.storeObjectType == StoreObjectType.ContactsFolder) { result = Folder.Create(context.Session, parentFolderObjectId, this.storeObjectType, displayName, CreateMode.CreateNew); } else if (this.storeObjectType == StoreObjectType.OutlookSearchFolder) { result = OutlookSearchFolder.Create(context.Session, displayName); } else { if (this.storeObjectType != StoreObjectType.SearchFolder) { throw new NotSupportedException(string.Format("The type of folder cannot be created. type = {0}.", this.storeObjectType)); } result = SearchFolder.Create(context.Session, parentFolderObjectId, displayName, CreateMode.CreateNew); } return(result); }
public static StoreObjectId Recreate(MailboxSession session, Guid searchFolderClsId) { if (session == null) { throw new ArgumentNullException("session"); } if (searchFolderClsId == Guid.Empty) { throw new ArgumentException("Guid is empty", "searchFolderClsId"); } using (Folder folder = Folder.Bind(session, DefaultFolderType.SearchFolders)) { using (QueryResult queryResult = folder.FolderQuery(FolderQueryFlags.None, null, null, new PropertyDefinition[] { InternalSchema.OutlookSearchFolderClsId })) { for (;;) { object[][] rows = queryResult.GetRows(10000); for (int i = 0; i < rows.Length; i++) { if (rows[i][0] is Guid && ((Guid)rows[i][0]).Equals(searchFolderClsId)) { goto Block_9; } } if (rows.Length <= 0) { goto Block_11; } } Block_9: throw new ObjectExistedException(ServerStrings.ExSearchFolderAlreadyExists(searchFolderClsId)); Block_11 :; } } VersionedId versionedId = OutlookSearchFolder.FindAssociatedMessageId(session, searchFolderClsId); if (versionedId == null) { throw new ObjectNotFoundException(ServerStrings.ExSearchFolderNoAssociatedItem(searchFolderClsId)); } StoreObjectId objectId; using (MessageItem messageItem = MessageItem.Bind(session, versionedId)) { bool deepTraversal; StoreId[] folderScope; Restriction restriction = OutlookSearchFolder.ReadOutlookSearchFolderDefinitionBlob(messageItem, out deepTraversal, out folderScope); QueryFilter searchQuery = FilterRestrictionConverter.CreateFilter(session, session.Mailbox.MapiStore, restriction, true); SearchFolderCriteria searchFolderCriteria = new SearchFolderCriteria(searchQuery, folderScope); searchFolderCriteria.DeepTraversal = deepTraversal; string valueOrDefault = messageItem.GetValueOrDefault <string>(InternalSchema.DisplayName, string.Empty); using (OutlookSearchFolder outlookSearchFolder = OutlookSearchFolder.Create(session, valueOrDefault)) { outlookSearchFolder[InternalSchema.OutlookSearchFolderClsId] = searchFolderClsId; FolderSaveResult folderSaveResult = outlookSearchFolder.Save(); if (folderSaveResult.OperationResult != OperationResult.Succeeded) { throw folderSaveResult.ToException(ServerStrings.ExCannotCreateFolder(folderSaveResult)); } outlookSearchFolder.Load(null); outlookSearchFolder.ApplyContinuousSearch(searchFolderCriteria); objectId = outlookSearchFolder.Id.ObjectId; } } return(objectId); }