如何从Exchange server2010 uisng托管API中的“公共文件夹”及其“子文件夹”中检索所有项目???
rootfolder = Folder.Bind(service,WellKnownFolderName.PublicFoldersRoot);
rootfolder.Load();
foreach (Folder folder in rootfolder.FindFolders(new FolderView(int.MaxValue)))
{
FindItemsResults<Item> findResults = folder.FindItems(iv);
foreach (Item item in findResults)
{
//get item info;
}
}
“如果我这样做,我不会在它的子文件夹中存在项目..公共文件夹也不支持深度遍历查询..如何从公用文件夹的子文件夹中获取项目???”
答案 0 :(得分:45)
要获取所有文件夹,请使用以下代码:
public void GetAllFolders(ExchangeService service, List<Folder> completeListOfFolderIds)
{
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView);
foreach (Folder folder in findFolderResults)
{
completeListOfFolderIds.Add(folder);
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
private void FindAllSubFolders(ExchangeService service, FolderId parentFolderId, List<Folder> completeListOfFolderIds)
{
//search for sub folders
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults foundFolders = service.FindFolders(parentFolderId, folderView);
// Add the list to the growing complete list
completeListOfFolderIds.AddRange(foundFolders);
// Now recurse
foreach (Folder folder in foundFolders)
{
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
获取所有项目:
List<Folder> completeListOfFolderIds = new List<Folder>();
//Fills list with all public folders and subfolders
GetAllFolders(service, completeListOfFolderIds);
foreach(Folder folder in completeListOfFolderIds)
{
ItemView itemView = new ItemView(int.MaxValue);
FindItemsResults<Item> searchResults = service.FindItems(folder.Id, itemView);
//do something with item list
}
答案 1 :(得分:3)
您需要执行公用文件夹树的递归遍历。您无法对公用文件夹执行深度遍历。
答案 2 :(得分:2)
public IEnumerable<Item> FindItemsInSubfolders(FolderId root, SearchFilter itemsFilter)
{
foreach (Folder folder in service.FindFolders(root, new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep }))
foreach(Item item in service.FindItems(folder.Id, itemsFilter, new ItemView(int.MaxValue)))
yield return item;
foreach (Item item in service.FindItems(root, itemsFilter, new ItemView(int.MaxValue)))
yield return item;
}
答案 3 :(得分:0)
List<Folder> completeListOfFolderIds = new List<Folder>();
GetAllFolders(service, completeListOfFolderIds);
foreach (Folder folder in completeListOfFolderIds)
{
List<Item> results = GetItems(service, folder);
}
public void GetAllFolders(ExchangeService service, List<Folder> completeListOfFolderIds)
{
ExtendedPropertyDefinition isHiddenProp = new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean);
FolderView folderView = new FolderView(int.MaxValue);
folderView.PropertySet = new PropertySet(isHiddenProp,FolderSchema.DisplayName, FolderSchema.Id, FolderSchema.ChildFolderCount, FolderSchema.TotalCount, FolderSchema.ParentFolderId, FolderSchema.WellKnownFolderName);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsEqualTo(isHiddenProp, false));
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());
Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
// Indicate a Traversal value of Deep, so that all subfolders are retrieved.
folderView.Traversal = FolderTraversal.Deep;
rootfolder.Load();
//FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderView);
FindFoldersResults findFolderResults = rootfolder.FindFolders(searchFilter, folderView);
foreach (Folder folder in findFolderResults)
{
completeListOfFolderIds.Add(folder);
//List<Item> results = GetItems(service, folder);
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
private void FindAllSubFolders(ExchangeService service, FolderId parentFolderId, List<Folder> completeListOfFolderIds)
{
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults foundFolders = service.FindFolders(parentFolderId, folderView);
completeListOfFolderIds.AddRange(foundFolders);
// Now recurse
foreach (Folder folder in foundFolders)
{
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
private List<Item> GetItems(ExchangeService service ,Folder folder)
{
if (service == null)
{
return null;
}
List<Item> items = new List<Item>();
PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties);
const Int32 pageSize = 50;
ItemView itemView = new ItemView(pageSize);
itemView.PropertySet = propertySet;
FindItemsResults<Item> searchResults = null;
try
{
do
{
searchResults = service.FindItems(folder.Id,itemView);
items.AddRange(searchResults.Items);
itemView.Offset += pageSize;
} while (searchResults.MoreAvailable);
}
catch (Exception ex)
{
}
return items;
}