sexta-feira, 20 de fevereiro de 2009

SPDisposeCheck Tool How To Use in "Post-Build"

SPDisposeCheck is a new tool to check your assemblies or exe that you use in SharePoint. but you can increase your potencial using it on "post-build events" in Visual Studio.

How:




Before install the tool, go to

"C:\Program Files\Microsoft\SharePoint Dispose Check\", you can check that this folder contains the file "SPDisposeCheck.exe.
To run spdisposecheck tool on "post built events" you must check the next steps:

- First add "SPDisposeCheck.exe" to "environment variables" or put the "SPDisposeCheck.exe" in System32 Folder.
Now your enviroment has ready to use SPDisposeCheck

The SPDisposeCheck tool only check assemblies or exe files, you must verify your output type.

In your project go to "Built Events" and add the following command line:

___________________________________________________
SPDisposeCheck "$(TargetPath)" -debug > c:\logs\CheckDisposeLog.txt
___________________________________________________


"$(TargetPath)" -> "c:\...\...\...\...\...\project\bin\(debug/release)\projectname.exe"
or
"$(TargetPath)" -> "c:\...\...\...\...\...\project\bin\(debug/release)\projectname.dll"

"> c:\logs\CheckDisposeLog.txt" -> location of your report

Its more simple for you use post built macros.









Now, built the solution and test it:
My code with the correct dispose:








Check log file











My Code without web dispose:








Check the log file:













hope this helps,

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

terça-feira, 23 de setembro de 2008

Get icon by file name (web part function)

This function obtains the extension of a file and returns a icon of Image gallery.
This stuff is very useful on custom WebParts to organize documents.
public static string GetIconByName(string myItemName)
{
string extension = string.Empty;
char[] arr = myItemName.ToCharArray();
int index=0;
for(int i = 0; i <>
if(arr[i]=='.')
index=i;
for(int j = index+1; j <>
extension+=arr[j];
if(File.Exists(Path.Combine(SPUtility.GetGenericSetupPath(@"template\images"),
string.Concat("IC", extension.ToUpper(), ".GIF"))))
return string.Concat("IC",
extension.ToUpper(), ".GIF");
else
{
return "ICgen.GIF";
}
}

sexta-feira, 13 de junho de 2008

SharePoint Escape Characters (Converter Functions)


SharePoint Escaped Function
This stuff is very useful to control a InternalName of the field, or verify the InternalName from the DisplayName in a list.

String format: “_x00” + + “_”


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

foreach (string myCharacter in charactersArrayString)
{
if (myTextToConvert.Contains(myCharacter))
{
myTextToConvert = myTextToConvert.Replace(myCharacter, myHexFunctionHelp(myCharacter));
}
}
return myTextToConvert;
}

private string myHexFunctionHelp(string charValue)
{
string myHexString = BitConverter.ToString(Encoding.Default.GetBytes(charValue));
string sharepointStartEscaped = "_x00";
string sharepointFinalEscaped = "_";
string result = sharepointStartEscaped + myHexString + sharepointFinalEscaped;
return result.ToLower();
}
ex:
sharepointEscapedCharactersConverter("Vários Números");
return “V_x00e1_rios_x0020_N_x00fa_meros”

URL-encoded Function
private string urlEncodedFunction(string myString)
{
string converted = HttpUtility.UrlEncode(myString);
return converted;
}


HTML-encoded Function


private string htmlEncodedFunction(string myString)
{
string converted = BitConverter.ToString(Encoding.Default.GetBytes(myString));
return converted;
}