2

I would like to know which programming language (Java or C#) can be used better for access to public folder from Microsoft Outlook. I am new to Outlook and have done researches about access to Outlook. And, I have found that C# is mostly used compared to Java. Moreover, some of APIs are not free to use. Is there any tutorial guides for each programming language so that I can follow and develop the application? Are there any open source libraries to be used?

These are the things I want to develop:

  • Access to Public Folders
  • View all the available folders
  • Get some data files from the folders

I am very grateful to your precious help! =)

Jin Ling
  • 1,333
  • 2
  • 13
  • 16

2 Answers2

4

In .NET (C#, etc) you can use the Exchange WebServices APIs to get access to the public folders by adding the NuGet package Microsoft Exchange WebServices to your application.

You'll need an instance of Microsoft.Exchange.WebServices.Data.ExchangeService to work with, plus a valid login for the server - passed in as a System.Net.NetworkCredential. For instance:

ExchangeService service = new ExchangeService();
service.AutodiscoverUrl("myemail@mycompany.com");
service.Credentials = new NetworkCredential("myemail", "mypassword", "MYDOMAIN");

Once you have that, public folders can be searched for using something like this:

public Folder GetFolder(string path)
{
    FolderView fview = new FolderView(100);
    fview.PropertySet = new PropertySet(BasePropertySet.IdOnly);
    fview.PropertySet.Add(FolderSchema.DisplayName);
    fview.Traversal = FolderTraversal.Shallow;

    SearchFilter filter = new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, path);

    var fldrs = exchange.FindFolders(WellKnownFolderName.PublicFoldersRoot, filter, fview);
    if (fldrs != null)
        return fldrs.FirstOrDefault();
}

That will return a folder in the root of your Public Folder tree by name. If you want to go deeper you can walk the tree using this method:

public Folder GetFolder(Folder src, string FolderName)
{
    FolderView fview = new FolderView(100);
    fview.PropertySet = new PropertySet(BasePropertySet.IdOnly);
    fview.PropertySet.Add(FolderSchema.DisplayName);
    fview.Traversal = FolderTraversal.Shallow;

    SearchFilter filter = new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, FolderName);

    var fldrs = src.FindFolders(filter, fview);
    if (fldrs == null)
        return null;

    return fldrs.FirstOrDefault();
}

You can monkey with the Traversal option and the SearchFilter to get the Exchange WebServices to do some of the work for you. My public folders are stored on a server in another country (not by my choice) so it was faster to do it this way. YMMV.

For all of the above you'll need to include the following:

using System.Net;
using Microsoft.Exchange.WebServices.Data;
James Toomey
  • 5,635
  • 3
  • 37
  • 41
Corey
  • 15,524
  • 2
  • 35
  • 68
  • Thanks a lot for your help! It is a very helpful reference for me. Thanks for Web Service suggestion. But when I try recursive traversal mentioned in [here](http://stackoverflow.com/questions/13877629/how-to-get-all-items-from-folders-and-sub-folders-of-publicfolders-using-ews-man), I have encountered Exception **There are no public folder servers available.** Is there any way I can solve it? – Jin Ling Mar 13 '14 at 03:48
  • what would go in src and foldername the parameters passed in GetFolder? – yatinbc Apr 03 '17 at 10:53
  • @yatinbc `src` is a `Folder` returned from `GetFolder` (either version) and `FolderName` is the name of a folder that you're looking for. – Corey Apr 03 '17 at 23:30
  • I am not able to get public folders – yatinbc Apr 04 '17 at 12:54
  • Thank you @JamesToomey for your edits. Appreciated. – Corey Dec 08 '19 at 08:32
0

Corey, Great example. I used your example and combined the two into a single procedure and wanted to share. You pass it a folder path like this:

Test Folders\Customer Responses

If the folder is not found, it returns null.

public static Folder GetPublicExchangeFolder(string folderPath, ExchangeService exchange)
{
  FolderView fview = new FolderView(1);
  fview.PropertySet = new PropertySet(BasePropertySet.IdOnly);
  fview.PropertySet.Add(FolderSchema.DisplayName);
  fview.Traversal = FolderTraversal.Shallow;

  Folder currentFolder = null; FindFoldersResults fldrs;

  string[] folders = folderPath.Split(new char[] { '\\' });
  foreach (string FolderName in folders)
  {
    SearchFilter filter = new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, FolderName);
    if(currentFolder==null)
      fldrs = exchange.FindFolders(WellKnownFolderName.PublicFoldersRoot, filter, fview);
    else
      fldrs = currentFolder.FindFolders(filter, fview);
    if ((fldrs == null) || (fldrs.Count()==0))
      return null;
    else
      currentFolder = fldrs.FirstOrDefault();
  }
  return currentFolder;
}

Like the other example, you need to connect to the exchange service.

ExchangeService exchange = new ExchangeService();
exchange .AutodiscoverUrl("myemail@mycompany.com");
exchange .Credentials = new NetworkCredential("myemail", "mypassword", "MYDOMAIN");

And include the following:

using System.Net;
using Microsoft.Exchange.WebServices.Data;

Enjoy!

J.J.

  • what would go in folderpath, the parameter passed in GetPublicExchangeFolder – yatinbc Apr 03 '17 at 10:54
  • A path from "All Public Folders" that you want to open. For example, if you wanted to open the folder "Faxes\Customer Service" which fall under "All Public Folders" in exchange, you would pass "Faxes\Customer Service" into the procedure. – J.J. WIllis Apr 04 '17 at 13:51
  • I am getting error as There are no public folder servers available. – yatinbc Apr 04 '17 at 13:56
  • what could be the reason – yatinbc Apr 04 '17 at 13:56