quarta-feira, 29 de outubro de 2008

Characters that you not use in sharepoint fields and sites. function

This function helps you to validate a internal site name or field name, how?
This Microsoft article helps you to understand http://support.microsoft.com/default.aspx?scid=kb;en-us;905231

This functions returns a string using those rules:

private static string ValidateSiteName(string toValidateName)
{
StringBuilder sb = new StringBuilder();
sb.Append(toValidateName.Trim());

for (int i = 0; i < arrayCharacters.Length; i++)
sb.Replace(arrayCharacters[i], "");
while (sb.Length != 0 && (sb[0] == '_' sb[0] == ' '))
sb.Remove(0, 1);
while (sb.Length != 0 && sb[sb.Length - 1] == '.')
{
sb.Remove(sb.Length - 1, 1);
while (sb.Length > 0 && sb[sb.Length - 1] == ' ')
sb.Remove(sb.Length - 1, 1);
}
return sb.Length > 50 ? sb.ToString().Remove(50) : sb.ToString();
}

Or use this,

public static string ValidateSiteName(string toValidateName)
{
StringBuilder sb = new StringBuilder();
sb.Append(toValidateName);
string[] arrayCharacters = { "~", "#", "%", "&", "*",
"{", "}",@"\", ":", "<", ">", "?", "/", "+", "", Convert.ToString('"') };
for (int i = 0; i < arrayCharacters.Length; i++)
sb.Replace(arrayCharacters[i],"_");
if (Convert.ToString(sb[0]) == "_")
sb.Replace(sb[0], ' ');
return sb.ToString();
}

enjoy!!!

Sem comentários: