Shimeon Shimeon - 1 month ago 15
C# Question

EWS folder path has different delimiter when returned from bound folder vs FindFolderResults

I am using EWS to retrieve the folder path from a Microsoft Exchange inbox folder. However, I am getting a different result when the folder path is retrieved using FindFolderResult vs retrieving the path from the folder directly. Please note that, in both cases, the path returned is correct but in the second case (retrieved form the folder directly) the delimiter that separates the folder names is some unknown character.

I am using Visual Studio 2013 and the target framework is 4.5. I have demonstrated the issue in a console application but it is also present in a WPF application.

Questions


  1. Does anyone know why the folder strings are presented differently and
    how to fix the problem?

  2. If no fix is available I will probably just find and replace the unknown character but I am not sure what
    it is. The character displays as "?" in the console window but is
    absent in debugging hover over (ie no delimiter between names) and shows as a "[?]" (box with a
    question mark inside) when printed to immediate using debug.print. Any thoughts?



Please see code snippet and output sample below.

Code Snippet

static void Main(string[] args)
{
string EmailAccount = "someemail@somedomain.com";

ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ServicePointManager.ServerCertificateValidationCallback =
CertificateValidationCallBack;
myService.UseDefaultCredentials = true;
myService.AutodiscoverUrl(EmailAccount, RedirectionUrlValidationCallback);

ExtendedPropertyDefinition PR_Folder_Path = new
ExtendedPropertyDefinition(26293, MapiPropertyType.String);
PropertySet myPropertySet = new PropertySet(BasePropertySet.IdOnly,
FolderSchema.DisplayName, PR_Folder_Path);

FolderView myFolderView = new FolderView(100);
myFolderView.PropertySet = myPropertySet;
myFolderView.Traversal = FolderTraversal.Deep;

FolderId targetFolder = WellKnownFolderName.Inbox;

FindFoldersResults myFolderResults =
myService.FindFolders(targetFolder, myFolderView);

foreach (var f in myFolderResults)
{
//Shows path text when retreived from FindFolderResults.
string s;
f.TryGetProperty(PR_Folder_Path, out s);
Console.WriteLine("Output from Folder Results: {0}", s);

//Binds the folder and show path text when retrieve from
//directly from the bound folder.
string t;
Folder myTestFolder = Folder.Bind(myService, f.Id, myPropertySet);
myTestFolder.TryGetProperty(PR_Folder_Path, out t);
Console.WriteLine("Output from Bound Folder : {0}", t);

}

Console.ReadLine();
}


Output Sample


Output from Folder Results: \Inbox\Personal

Output from Bound Folder : ?Inbox?Personal

Output from Folder Results: \Inbox\Personal\Contacts

Output from Bound Folder : ?Inbox?Personal?Contacts


What fixed the issue for me

string t;
string c;
Folder myTestFolder = Folder.Bind(myService, f.Id, myPropertySet);
myTestFolder.TryGetProperty(PR_Folder_Path, out t);

byte[] tBytes = Encoding.Default.GetBytes(t);
var hexString = BitConverter.ToString(tBytes);
hexString = hexString.Replace("3F", "5C");
c = System.Text.Encoding.Default.GetString(StringToByteArray(hexString));


Other posts that helped

Convert string to hex-string in C#

How can I convert a hex string to a byte array?

Answer

Its the Byte-Order Mark that is being returned there are numerous ways of replacing them in a String i use

Encoding.Unicode.GetString(HexConvertor.HexStringToByteArray(BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes((String)FolderPathValue)).Replace("FE-FF", "5C-00")

but there are other ways of doing that maybe cleaner and faster

The thing to be careful around using that property in FindFolder is that if the size of the value is greater then 256 bytes it will be truncated and you will need to do Bind/GetFolder to get the whole value.