4

By default SharePoint does not allow the following special characters to be present in attachment file names

~, #, %, & , *, {, }, \, :, <, >, ?, /, |, “

How can I validate that any attachments file name does not have ANY special characters(not just the ones above, but all special characters)?

2
  • Are you fine with PowerShell code? – Asad Refai Sep 18 '15 at 6:19
  • 1
    Powershell wont help with validation on upload. You could use it to check (and change) your files prior to uploading... Make sure to develop and test,test,test on a copy of your files, I once ruined a fileshare..and.. you guessed it... the backup was 2 days old – Danny '365CSI' Engelman Sep 18 '15 at 7:06
2

All of the characters you list are non-word characters. They can be detected by using this Regex:

^\W|[^\w]

^\W means:

Matches any string that starts with a non-word character

[^\w] means:

Matches any characters that are non-word characters

1

If you can deploy a visual studio solution, then you can use an Event Receiver.

I have used an array to store all the chars I want to skip/validate:

public static string[] invalidChars = new string[] { ";", "#", "/", "~", "#", "%", "&", "*", "{", "}", ":", "<", ">", "?", "¿", "+", "|", "^", "'", "@", "[", "]", ",", "$", "=", "!", "¡", "(", ")", "_", "\\" }; 

And then in the Event Receiver on "ItemAdding":

try
{
     SPList myList = __properties.Web.Lists["your list"];
     SPListItem myItem = myList.GetItemById(__properties.ListItemId.ToString());
     foreach (string myAttach in myItem.Attachments)
     {
          foreach (string invalidChar in invalidChars)
          {
               if (myAttach.Contains(invalidChar))
               {
                    throw new Exception("Invalid Character: " + invalidChar + "in filename: " + myAttach);
               }    
          }
     }
}
catch (Exception ex)
{
     properties.Cancel = true;
     properties.Status = SPEventReceiverStatus.CancelWithError;
     properties.ErrorMessage = ex.Message;
     throw new Exception(ex.Message);
}
1
  • There's a way if you are using InfoPath to customize your form too. Just let me know. Hope to help. – Marcelo Espinosa Sep 18 '15 at 16:15

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.