问题
I have a folder in an Exchange mailbox that is a child of the root and is created by user.
How do I find such a folder using EWS managed API?
I tried using deep traversal, but I can't find the folder.
Edit: Here is the code I am using to get the folder created by user:
ExchangeService server = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
server.UseDefaultCredentials = true;
string configUrl = @"https://yourServerAddress.asmx";
server.Url = new Uri(configUrl);
// set View
FolderView view = new FolderView(100);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;
FindFoldersResults findFolderResults = server.FindFolders(WellKnownFolderName.Root, view);
// find specific folder
foreach (Folder f in findFolderResults)
{
// show FolderId of the folder "test"
if (f.DisplayName == "Test")
{
Console.WriteLine(f.Id);
}
}
回答1:
You should include the code your using in your question as you probably just have bug in that. What i do is use a function to find the folder from a string path then you can just call that like GetFolderFromPath(service,"mailbox@domain.com","\\folder1\Folder2") eg
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 > 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");
}
}
回答2:
Apparently, your code is correct but it will not give you access to the exchange severer. I have faced the same issue and fixed these lines only.
Actually MS ignores the
UseDefaultCredentials = true;
That may be the reason you're not logged into the exchange server. See the MS documentation here for detail.
Use this approach:
var exchange = new MSEWS.ExchangeService(MSEWS.ExchangeVersion.Exchange2007_SP1);
// userid, password, and your network domain
exchange.Credentials = new MSEWS.WebCredentials(userName, password, domain);
exchange.AutodiscoverUrl("tushar.kapoor@bollywood.com");
来源:https://stackoverflow.com/questions/39832366/exchange-web-service-folderid-for-a-folder-created-by-user