SharePoint filename special characters not allowed

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.

 

[tweetmeme only_single=”false”]
Advertisements

16 thoughts on “SharePoint filename special characters not allowed

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

  1. 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(“..””, “.”);
        }

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s