Title: Search specific folder on Exchange Server
Tags: c#;api;exchange-server;exchange-server-2010
Question: I have a block of code that currently reads all emails on an exchange server from the general "Inbox" folder. The line below is what is accessing that particular folder
FindItemsResults<Item&gt; findResults = exchange.FindItems(WellKnownFolderName.Inbox, new ItemView(250));
Question
is it possible to adapt this code so that I can search a user created folder instead of the generic inbox? for example I have created a folder called "Test Folder", and I want to be able to read all emails in here.
I have tried adding a Search Filter but this returns no results at present:
SearchFilter searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Test Folder");
FindItemsResults<Item&gt; findResults = exchange.FindItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(250));

Here is the accepted answer: You need to first find the FolderId of the user created Folder in question then you can either use that in the FindItem method on the service class using the FolderId or use the FindItem method on the folder itself. Eg to find the user created folder you can convert it to a path and search for that path
internal static Folder GetFolderFromPath(ExchangeService service,String MailboxName,String FolderPath)
FolderId folderid = new  FolderId(WellKnownFolderName.MsgFolderRoot,MailboxName);
Folder tfTargetFolder = Folder.Bind(service,folderid);
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
String[] fldArray = FolderPath.Split('\\');
for (Int32 lint = 1; lint < fldArray.Length; lint++) {
FolderView fvFolderView = new FolderView(1);
fvFolderView.PropertySet = psPropset;
SearchFilter  SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,fldArray[lint]);
FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id,SfSearchFilter,fvFolderView);
if (findFolderResults.TotalCount &gt; 0){
foreach(Folder folder in findFolderResults.Folders){
tfTargetFolder = folder;
}
}
else{
tfTargetFolder = null;
break;
}
if (tfTargetFolder != null)
return tfTargetFolder;
else
throw new Exception("Folder Not found");
So if the folder was within the Inbox you would use
GetFolderFromPath(service,"pramirez@example.org","\Inbox\folder") which should then return that folder.
Comment for this answer: thankyou very much, this will help the application run more efficiently


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:05/16/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><api><exchange-server><exchange-server-2010>', 'Title': 'Search specific folder on Exchange Server', 'ViewCount': 1008, 'id': 43997189, 'user_AboutMe': '', 'user_AccountId': 3543434.0, 'user_DisplayName': 'Crezzer7', 'user_Reputation': 2185.0, 'user_Views': 231.0, 'user_id': 2959978.0}}