Determine if a File Type will be blocked programmatically

If you are writing custom code that uploads a document into a SharePoint Document Library, you must abide by the same rules as if uploading the document through the SharePoint UI. Therefore, if the user tries to upload a document that is of a restricted file type, they will receive an error.

“The following file(s) have been blocked by the administrator: filename”.

You can control which files are blocked from Central Administration. Browse to Operations > Blocked File Types (Under the Security Configuration Group)

When uploading files programmatically, you do not have to write any code to check which file types are allowed before uploading a file. SharePoint will return the same error if you leave your code as-is and you try to upload a blocked file type. Be sure to handle this exception properly so your user knows what is going on.

If you wish to check beforehand you can do so by reading through SPContext.Current.Site.WebApplication.BlockedFileExtensions

Here is a rough example:

public static bool IsFileTypeBlocked(string extension)
{
System.Collections.ObjectModel.Collection<String> types = SPContext.Current.Site.WebApplication.BlockedFileExtensions;
bool blocked = false;

foreach(string type in types)
{
if (extension.Equals(type))
{
blocked = true;
break;
}
}
return blocked;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.