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!!!

domingo, 19 de outubro de 2008

How to get SharePoint users from peoplePicker?

Get peoplePicker string text, and convert it to an int array list of users’ index


public static int[] GetUserIndexesFromPeoplePicker(string users)
{
int nUsers;
int[] indexes;
if (users == null users.Trim().Length == 0)
return null;
string[] splited = users.Split(new char[] { '#', ';' },
StringSplitOptions.RemoveEmptyEntries);
if (splited.Length < 2 splited.Length % 2 != 0)
return null;
nUsers = splited.Length / 2;
indexes = new int[nUsers];
for (int i = 0, j = 0; j < nUsers; i += 2, ++j)
indexes[j] = int.Parse(splited[i]);
return indexes;
}


Or, get a string text from peoplePicker and convert it to a string array list of login names,


public static string[] GetUserNamesFromPeoplePicker(string users)
{
int nUsers;
string[] userNames;
if (users == null users.Trim().Length == 0)
return null;
string[] splited = users.Split(new char[] { '#', ';' },
StringSplitOptions.RemoveEmptyEntries);
if (splited.Length < 2 splited.Length % 2 != 0)
return null;
nUsers = splited.Length / 2;
userNames = new string[nUsers];
for (int i = 1, j = 0; j < nUsers; i += 2, ++j)
userNames[j] = splited[i];
return userNames;
}


Enjoy!!!

SharePoint Escaped Characters (Converter Functions) NEW VERSION

SharePoint Escaped Function

This stuff is very useful to control a Internal Name of the fields,
or verify the Internal Name from the Display Name in a list.

It’s usual, the internal names of the fields do not have more than 32 characters.

The string array list with all characters

private static string[] charactersArrayString =
{"ã","á","à","â","ä","Ã","Á","À","Â","Ä","é","è","ê","ë", "É","È",
"Ê","Ë","í","ì","î","ï","Í","Ì","Î","Ï","ó","ò",
"ô","ö","õ","Ò","Ó","Ô","Ö","Õ","ú","ù","û","ü","Ù","Ú",
"Û","Ü","ç","Ç","ñ","Ñ","ª","º","(",")","!",@"\",@",","$",
"%","&", "/","=","?","@","£","§","{","[","]","}","?","»",
"«","»","~","+","*",";","","¨",":"," ","-"};
The function to create or guess what is the field name

public static string EscapedSpecialCharacters(string myTextToConvert)
{
foreach (string myCaracter in charactersArrayString)
if (myTextToConvert.Contains(myCaracter))
myTextToConvert = myTextToConvert.Replace(myCaracter,
myHexFunctionHelper(myCaracter)); return myTextToConvert.Length > 32 ?
myTextToConvert.Remove(32) : myTextToConvert;
}

Enjoy!!!