Normalizing And Denormalizing SharePoint Field Names
Frequently, when working with Office, SharePoint, and SharePoint web services, it is necessary to convert between the “normalized” (hex escaped string) version of a field name.
To that end, I found a useful JavaScript tool for normalizing strings into SharePoint’s “static name” format.
In .Net, we can simplify this using Regex and Uri.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<span style="font-family: Courier New;"> <span style="color: #0000ff;">private static readonly </span><span style="color: #808000;">Regex </span><span style="color: #000000;">specialCharactersPattern</span> <span style="color: #000000;">= </span><span style="color: #0000ff;">new </span><span style="color: #808000;">Regex</span><span style="color: #000000;">(</span><span style="color: #ff00ff;">"[\\[*($%&)<>!?\\/\"{}\\s+-='@~`#\\\\:;^\\]]"</span><span style="color: #000000;">, </span><span style="color: #808000;">RegexOptions</span><span style="color: #000000;">.Compiled);</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">private static readonly </span><span style="color: #808000;">Regex </span><span style="color: #000000;">encodedCharactersPattern</span> <span style="color: #000000;">= </span><span style="color: #0000ff;">new </span><span style="color: #808000;">Regex</span><span style="color: #000000;">(</span><span style="color: #ff00ff;">"_x00(\\d{2})_"</span><span style="color: #000000;">, </span><span style="color: #808000;">RegexOptions</span><span style="color: #000000;">.Compiled);</span></span> <span style="font-family: Courier New;"> <span style="color: #008000;">/// <summary></span> <span style="color: #008000;">/// Normalizes the name of the SharePoint property name.</span> <span style="color: #008000;">/// </summary></span> <span style="color: #008000;">/// <param name="key">The "human readable" key/property name.</param></span> <span style="color: #008000;">/// <returns></span> <span style="color: #008000;">/// The string with hex representation in place of special ASCII</span> <span style="color: #008000;">/// characters.</span> <span style="color: #008000;">/// </returns></span> <span style="color: #0000ff;">private static string </span><span style="color: #000000;">NormalizeSharePointPropertyName(</span><span style="color: #0000ff;">string </span><span style="color: #000000;">key) {</span> <span style="color: #0000ff;">return </span><span style="color: #000000;">specialCharactersPattern.Replace(key, ReplaceSpecialCharacter);</span> <span style="color: #000000;">}</span></span> <span style="font-family: Courier New;"> <span style="color: #008000;">/// <summary></span> <span style="color: #008000;">/// Custom match evaluator to replace special characters within the input</span> <span style="color: #008000;">/// string.</span> <span style="color: #008000;">/// </summary></span> <span style="color: #008000;">/// <param name="match">The pattern match.</param></span> <span style="color: #008000;">/// <returns>The formatted version of the string.</returns></span> <span style="color: #0000ff;">private static string </span><span style="color: #000000;">ReplaceSpecialCharacter(</span><span style="color: #808000;">Match </span><span style="color: #000000;">match) {</span> <span style="color: #0000ff;">string </span><span style="color: #000000;">replacement = </span><span style="color: #0000ff;">string</span><span style="color: #000000;">.Format(</span><span style="color: #ff00ff;">"_x00{0}_"</span><span style="color: #000000;">,</span> <span style="color: #808000;">Uri</span><span style="color: #000000;">.HexEscape(match.Value[</span><span style="color: #800080;">0</span><span style="color: #000000;">]).TrimStart(</span><span style="color: #ff00ff;">'%'</span><span style="color: #000000;">));</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">return </span><span style="color: #000000;">replacement;</span> <span style="color: #000000;">}</span></span> <span style="font-family: Courier New;"> <span style="color: #008000;">/// <summary></span> <span style="color: #008000;">/// Denormalizes the name of the SharePoint property.</span> <span style="color: #008000;">/// </summary></span> <span style="color: #008000;">/// <param name="key">The normalized key/property name (static name).</param></span> <span style="color: #008000;">/// <returns></span> <span style="color: #008000;">/// The denormalized form of the input string ("human readable"</span> <span style="color: #008000;">/// with hex patterns replaced).</span> <span style="color: #008000;">/// </returns></span> <span style="color: #0000ff;">private static string </span><span style="color: #000000;">DenormalizeSharePointPropertyName(</span><span style="color: #0000ff;">string </span><span style="color: #000000;">key) {</span> <span style="color: #0000ff;">return </span><span style="color: #000000;">encodedCharactersPattern.Replace(key, DecodeSpecialCharacter);</span> <span style="color: #000000;">}</span></span> <span style="font-family: Courier New;"> <span style="color: #008000;">/// <summary></span> <span style="color: #008000;">/// Custom match evaluator to replace the hex characters with special characters.</span> <span style="color: #008000;">/// </summary></span> <span style="color: #008000;">/// <param name="match">The pattern match.</param></span> <span style="color: #008000;">/// <returns>The ASCII format of the special character encoded in hex.</returns></span> <span style="color: #0000ff;">private static string </span><span style="color: #000000;">DecodeSpecialCharacter(</span><span style="color: #808000;">Match </span><span style="color: #000000;">match) {</span> <span style="color: #0000ff;">int </span><span style="color: #000000;">start = </span><span style="color: #800080;">0</span><span style="color: #000000;">;</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">string </span><span style="color: #000000;">replacement =</span> <span style="color: #808000;">Convert</span><span style="color: #000000;">.ToString(</span> <span style="color: #808000;">Uri</span><span style="color: #000000;">.HexUnescape(</span><span style="color: #808000;">Convert</span><span style="color: #000000;">.ToString(match.Value[</span><span style="color: #800080;">0</span><span style="color: #000000;">]), </span><span style="color: #0000ff;">ref </span><span style="color: #000000;">start)</span> <span style="color: #000000;">);</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">return </span><span style="color: #000000;">replacement;</span> <span style="color: #000000;">}</span></span> |
Download the sample console project to test it out.