Using folders in SharePoint could be a good thing. Microsoft recommends using folders for large lists or libraries. But sometimes we do not want folders in a list or library. So, the first thing to do is, hiding the “New Folder”-entry from the New-menu in the toolbar of the library. This could easily be done in the advanced settings.
By setting this option to “No”, the entry “New Folder” will not appear in the New-menu.
Cool, but what shall we do, when a user opens the library using his Windows Explorer? From there, he can right-click and choose New > Folder. So we can prevent creating folders via the web interface, but this has no effect using other utilities.
When you ask Google, what to do to prevent creating folders, you may find results saying to use an event handler and implement the ItemAdding-event. Just look for the content type in the AfterProperties and if it is “Folder” just cancel further processing.
I investigated the properties-parameter in the ItemAdding-event in several situations, but I never saw a content type in the AfterProperties. But I saw another interesting thing: when a document should be added, we have a value for a property named “vti_filesize” in the AfterProperties. When a folder is created, this property is missing.
So, a possible option could be, to look for the vti_filesize-property in the AfterProperties, when handling the ItemAdding-event. So the ItemAdding-method could look like this:
After deploying our assembly to the Global Assembly Cache, we just have to register the event-handler in our library. That’s all.