Exchange Web Service FolderId for a folder created by user

时光毁灭记忆、已成空白 提交于 2020-01-25 21:57:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!