MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

Validating Sharepoint filenames on upload

This is Crazy! I was just talking with Joseph Mortimer @ Data#3 about building an application to clean file names before uploading to SharePoint. Funny!
Via Mads

Some characters are legal for FAT or NTFS files but illegal for files in Sharepoint documentlibraries. Using the regular upload UI for Sharepoint you'll encounter a rather unpleasant validation error when uploading a file with funky characters. Using the object model will give you a good ole' exception.

I've seen a lot of approaches to solving this problem, and implemented a couple myself. Last night I noticed a method on the SPEncode static class that enabled a clean approach to the problem:

private string CleanFileName(string dirtyFileName)

{

    char[] dirtyChars = dirtyFileName.ToCharArray();

    foreach (char c in dirtyChars)

    {

        if (!SPEncode.IsLegalCharInUrl(c))

        {

            dirtyFileName = dirtyFileName.Replace(c.ToString(), "");

        }

    }

   

    return dirtyFileName; // now clean:-)

}

 


Posted Nov 17 2005, 08:39 PM by anguslogan

Add a Comment

(required)  
(optional)
(required)  
Remember Me?


Copyright © is the original authors. Blog site is an independent site not sponsored by Microsoft. The Yoda blog server and the Brianna SQL server would like to thank www.ownwebnow.com and www.exchangedefender.com. They wouldn't be here and broadcasting without the generosity of Vlad Mazek and his companies.

Powered by Community Server (Commercial Edition), by Telligent Systems