0

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));

    public static byte[] StringToByteArray(string hex)
    {
        hex = hex.Replace("-", "");
        return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                         .ToArray();
    }

Other posts that helped

Convert string to hex-string in C#

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

Community
  • 1
  • 1
Shimeon
  • 243
  • 2
  • 9

1 Answers1

0

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.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thanks for your response. I will likely give it a try later today. So what you are saying is, even though FindFolderResults is giving me the back slashes the way I want, I should use the Folder.Bind method (the one that is currently returning the Byte-Order Mark delimiter) as it is safer (ie no chance of truncation)? – Shimeon Oct 28 '16 at 13:34
  • You only need to use it where the length exceed 256 characters which is pretty rare give the length of the path – Glen Scales Oct 31 '16 at 01:39
  • For some reason I couldn't get your code to work in its original form. Perhaps I was missing a Using or something. Anyway it pointed me in the right direction. I have posted the code I used (which is pretty much doing exactly what you suggested) along with some links I found helpful. – Shimeon Oct 31 '16 at 16:16