SharePoint - Internal field names in Hebrew
A field (sometimes called a column) in SharePoint - represented in the object model by the SPField class, has two main properties which are used to identify it in code, the Title property and the InternalName property, the reason for the duplication is that the Title property can be changed from the GUI or translated into other languages, therefore if we want to write code which can be used without recompiling after every field name change in the GUI or recompiling for different languages we will use the InternalName .
For example:
- If you want to access the field which holds the last modification date of a list item you will use the InternalName property which has the value Modified and not on the Title which has the value Modified in English and השתנה in Hebrew.
- SPListItemCollection.GetDataTable() will return a DataTable with internal names as columns.
After understanding why it’s important to used internal names now let’s talk about the surprises, when you create a field in a list of a document library using the GUI, the name that was typed in the creation will be used as an internal name after some sort of encoding f.e. space character is encoded to _x0020_ Hebrew characters are also encoded if I create a field named שלום עולם the InternalName will be the following monster:
_x05e9__x05dc__x05d5__x05dd__x0020__x05e2__x05d5__x05dc__x05dd_
I wrote a function to do the transform between the encoded version and the decoded version.
The Encode function:
/// <summary>
/// This function converts strings in Hebrew (it can be used for other
/// languages too) to the hexadecimal presentation that is used by SharePoint
/// for internal field names
/// </summary>
/// <param name="inputString">The string to convert</param>
/// <returns>The converted string</returns>
public static string ConvertHebrewToUnicodeHex(string inputString)
{
StringBuilder outputString = new StringBuilder();
//convert the string to char array and manipulate the chars
char[] charArray = inputString.ToCharArray();
foreach (char c in charArray)
{
int charIntRepresentation = c;
outputString.Append("_x" + String.Format("{0:x4}", (uint)System.Convert.ToUInt32(charIntRepresentation.ToString())) + "_");
}
return outputString.ToString();
}
The Decode function:
/// <summary>
///This function converts strings in the unicode hexadecimal presentation
/// for Hebrew that is used by SharePoint to a string in Hebrew
/// </summary>
/// <param name="inputString">The string to convert</param>
/// <returns>The converted string</returns>
public static string ConvertUnicoeHexToString(string inputString)
{
StringBuilder outputString = new StringBuilder();
//Each char is represented in the following format
// _x????_ (7 chars long) where ???? is number in hex
for (int i = 0; i < inputString.Length; i += 7)
{
string hexValue = inputString.Substring(i, 7).Substring(2, 4);
char charCode = (char)UInt32.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
outputString.Append(charCode);
}
return outputString.ToString();
}
Have fun…
David Birin