1

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?

1
  • 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. Commented Aug 14, 2014 at 7:07

1 Answer 1

0

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

var encFolderName = SPEncode.HtmlEncode(folderName);

Update

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.

2
  • 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. ~ " # % & * : < > ? / \ { | }.) Commented Aug 14, 2014 at 23:37
  • The answer has been updated, SPHttpUtility.UrlPathEncode method probably is the best choice here Commented Aug 16, 2014 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.