« powrót

Zamiana nazwy wyświetlanej na wewnętrzną dla pola listy [Sharepoint / C#]

Opublikowano: 2019-08-20 , wyświetlono: 2239

Przy pisaniu kodu dla zastanych już definicji list, czasem trzeba zamienić nazwę wyświetlaną pola na wewnętrzną nazwę używaną przez platformę, Poniżej prosta funkcja, która rozwiązuje ten problem.


/// <summary>
/// convert display name to internal name
/// </summary>
/// <param name="toEncode"></param>
/// <returns></returns>
public static string EncodeToInternalField(string toEncode)
{
    if (toEncode != null)
    {
        StringBuilder encodedString = new StringBuilder();

        foreach (char chr in toEncode.ToCharArray())
        {
            string encodedChar = HttpUtility.UrlEncode(chr.ToString());

            if (encodedChar.StartsWith("%"))
            {
                encodedChar = encodedChar.Replace("u", "x");
                encodedChar = encodedChar.Substring(1, encodedChar.Length - 1);
                encodedChar = String.Format("_{0}_", encodedChar);
                encodedString.Append(encodedChar);
            }
            else if (encodedChar == "+" || encodedChar == " ")
            {
                encodedString.Append("_x0020_");
            }
            else if (encodedChar == ".")
            {
                encodedString.Append("_x002e_");
            }
            else
            {
                encodedString.Append(chr);
            }

        }
        return encodedString.ToString();
    }
    return null;
}



Komentarze: