I need to programmatically create a SP Folder (this is not via Object Model!)

The SP folders do not allow

The file or folder name ... contains invalid characters. Please use a different name. Valid file or folder names cannot begin or end with a dot, cannot contain consecutive dots and cannot contain any of the following characters: ~ " # % & * : < > ? / \ { | }.

It does not appear that there exists a .FolderEncode(string folderName) / .FolderDecode(string folderName)

So what would the best approach be. To use a regex to remove the chars? Bearing in mind I will need to write this as .Decode and .Encode in either approach (or maybe just on the way in / on insert... need to think about this...) any ideas?

  • The best approach I think would be to validate the input. If the is a UI you have one validation on that level and then you have another level of validation in the code that creates the folder and throw an argument exception for invalid inputs. – Robban1980 Aug 14 '14 at 7:07

You could use SPEncode.HtmlEncode method to encode the specified string that contains special characters, for example:

var encFolderName = SPEncode.HtmlEncode(folderName);


SPHttpUtility.UrlPathEncode is a SharePoint specific method that encodes the specified path part of a URL, for example:

var encFolderName = SPHttpUtility.UrlPathEncode(folderName,false);

URL Encoding using C# question contains a great answer with detailed expanantion and comparison of different URL encoding methods available in .NET.

  • I have tried this but unfortunately won't work because all of the SPEncode.xxxxEncode methods still output chars that sharepoint folder is not happy with (i.e. ~ " # % & * : < > ? / \ { | }.) – user32469 Aug 14 '14 at 23:37
  • The answer has been updated, SPHttpUtility.UrlPathEncode method probably is the best choice here – Vadim Gremyachev Aug 16 '14 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.