I was creating files in memory and loading them into a doc library based on user input on a webpart.
Some smart user put special characters in and of course it blew up my web part.
So I had to research what characters are not allowed in file names.
Here they are:
~, #, %, & , *, {, }, \, :, <, >, ?, /, |, “
So I wrote a little function to remove the special characters.
public static string RemoveSpecialCharacters(string stringToConvert)
{
stringToConvert = stringToConvert.Replace(“~”, string.Empty);
stringToConvert = stringToConvert.Replace(“#”, string.Empty);
stringToConvert = stringToConvert.Replace(“%”, string.Empty);
stringToConvert = stringToConvert.Replace(“&”, string.Empty);
stringToConvert = stringToConvert.Replace(“*”, string.Empty);
stringToConvert = stringToConvert.Replace(“{“, string.Empty);
stringToConvert = stringToConvert.Replace(“}”, string.Empty);
stringToConvert = stringToConvert.Replace(“\\”, string.Empty);
stringToConvert = stringToConvert.Replace(“:”, string.Empty);
stringToConvert = stringToConvert.Replace(“<“, string.Empty);
stringToConvert = stringToConvert.Replace(“>”, string.Empty);
stringToConvert = stringToConvert.Replace(“?”, string.Empty);
stringToConvert = stringToConvert.Replace(“/”, string.Empty);
stringToConvert = stringToConvert.Replace(“|”, string.Empty);
stringToConvert = stringToConvert.Replace(“\””, string.Empty);
}
So any filename I was going to use got run through this function to clean it up.
Chris,
This problem never seems to go away. I wrote a similar routine 25 years ago on a VMS system. Good job!
why cannot resolve it by the product?
exactly sharepoint should resolve the conflict with windows 7 file naming conventions. The user doesn’t care about url restrictions do an automatic character escape conversion that is invisible to the end user microsoft!!!
Did you create a new web part to upload document?
There’s something this won’t catch – SharePoint doesn’t allow you to have two full stop (period) characters immediately before the extension, i.e. “Myfile.doc” is OK, but “Myfile..doc” is NOT (even though this is legal in the Windows file system).
Here is an updated function to replace .. with .
I did not test this.
public static string RemoveSpecialCharacters(string stringToConvert)
{
stringToConvert = stringToConvert.Replace(“~”, string.Empty);
stringToConvert = stringToConvert.Replace(“#”, string.Empty);
stringToConvert = stringToConvert.Replace(“%”, string.Empty);
stringToConvert = stringToConvert.Replace(“&”, string.Empty);
stringToConvert = stringToConvert.Replace(“*”, string.Empty);
stringToConvert = stringToConvert.Replace(“{“, string.Empty);
stringToConvert = stringToConvert.Replace(“}”, string.Empty);
stringToConvert = stringToConvert.Replace(“\\”, string.Empty);
stringToConvert = stringToConvert.Replace(“:”, string.Empty);
stringToConvert = stringToConvert.Replace(“”, string.Empty);
stringToConvert = stringToConvert.Replace(“?”, string.Empty);
stringToConvert = stringToConvert.Replace(“/”, string.Empty);
stringToConvert = stringToConvert.Replace(“|”, string.Empty);
stringToConvert = stringToConvert.Replace(“\””, string.Empty);
stringToConvert = stringToConvert.Replace(“..””, “.”);
}
Hello,
That won’t work with a filename like this “hello….txt” – you need to use a while loop. Something like this (not tested):
while (stringToConvert.Contains(“..”))
{
stringToConvert = stringToConvert.Replace(“..””, “.”);
}
Hi,
do you use this function within SharePoint (which web part?) or did you create a windows form ?
I use this function when I create a custom application form for input. Deployed through a feature,
what characters are not allowed in sharepoint 2010 search
The special charaters not allowed are:
~
#
%
&
*
{
}
\\
:
?
/
|
\
how do i download the pdf?
Sorry that was spam.
Will SP 2010 search function find a document with no separation? Thanks Ty
I’m not sure I know what you mean when you say “no separation”?
If you are talking about _ for word separation, SharePoint Foundation will not find it, but all other version of SharePoint will.
Thank you for your help 🙂