views:

156

answers:

2

Is there a way to programmatically determine a file type in SharePoint? I want to limit the types of files that are being uploaded into a document library. I have written an EventReceiver that on ItemAdding conducts the following -

if (!(properties.AfterUrl.Contains(".docx") || properties.AfterUrl.Contains(".pptx") || properties.AfterUrl.Contains(".xlsx") ))

Surely there's a better way to do so?

A: 

Blocking file types is only possible at the farm level (through the central admin).

An Event Handler checking the file's extension is the only way to go if you want to be able to administer this at a document library level.

So no, there is no better way of doing this.

Colin
There's no better way to check a file type other than check the extension on the file?
esp
No, not at the moment ItemAdding is fired. Since it's executed before the actual item is created (an SPListItem is at it's core nothing more than a SQL record), there is no SPFile object yet you could use (like say to create a FileInfo object). And, in the end, isn't this how most system restrict the uploading of files? By chcking it's extension?
Colin
A: 

If you are really interested in restricting certain types of files, I would recommend to go beyond file's extension or mime types and inspect file's content to determine its nature, which is what IE and Firefox do.

(BTW, there's an IE API whose name I cannot remember right now that gives you the mime type of a file after inspecting it.)

Ariel