SharePoint, like the file system, does not allow all characters in a file and folder name. Next to these characters there are also some limitations on the first characters of a name (e.g. _ makes it hidden) and you cannot use some endings.
I found some examples, but these didn’t work for me. So with the help of regexone.com and this StackExchange post, I created my own.
To be honest, this blog is just an easy place to find it again if I need it.
The code:
1 2 3 4 5 6 |
private static string SanitizeName(string name) { var pattern = new Regex(@"^([\._])*|(\.*|\.files|_files|-Dateien|_fichiers|_bestanden|_file|_archivos|-filer|_tiedostot|_pliki|_soubory|_elemei|_ficheiros|_arquivos|_dosyalar|_datoteke|_fitxers|_failid|_fails|_bylos|_fajlovi|_fitxategiak)$|(\.{2,})|([~#%&*{}\<>?/|""])"); string output = pattern.Replace(name, ""); return output; } |
Regex is not something I’m very familiar with. So if anybody has a suggestion, please comment.