Thursday, March 10, 2011

Using the PR_Folder_Path property in Exchange Web Services to show the folder path

The PR_Folder_PathName Mapi property is a useful property to use when migrating an application that uses WebDAV to EWS where the webdav application has used the DAV:href property paths or if you just want to display the fullpath to a folder delimited by "\" which may useful for other applications.

The PR_Folder_PathName property returns the fullpath to a folder that is delimited by the byte order mark FE-FF so if you want to return a \ delimited path you need to replace these values in the Unicode string that is returned when you retrieve this property.

eg in the managed API

static void getPath(ExchangeService service) {
ExtendedPropertyDefinition PR_Folder_Path = new ExtendedPropertyDefinition(26293, MapiPropertyType.String);
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties) { PR_Folder_Path };
Folder foFolder = Folder.Bind(service, WellKnownFolderName.RecoverableItemsDeletions, psPropSet);
Object fpPath = null;
foFolder.TryGetProperty(PR_Folder_Path,out fpPath);
String fpPathString = Encoding.Unicode.GetString(HexStringToByteArray(BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes((String)fpPath)).Replace("FE-FF", "5C-00").Replace("-", "")));
Console.WriteLine(fpPathString);
}

static Byte[] HexStringToByteArray(String HexString)
{
Byte[] ByteArray = new Byte[HexString.Length / 2];
for (int i = 0; i < HexString.Length; i += 2)
{
ByteArray[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
}
return ByteArray;
}

1 comment:

Anonymous said...

I'd recommend the following:

string delimiter = Encoding.Unicode.GetString(new byte[] { 0xFE, 0xFF });
string fpPathString = ((string)fpPath).Replace(delimiter, "\\");

This way you avoid converting the string to a byte array and vice versa...