Technical Fun

Preventing Folder Creation on MOSS 2007 Document Libraries

Posted on: January 28, 2008

Suddenly my team mate got this strange need to prevent folder creation inside MOSS document libraries. So I said “Aah .. “, and contemplated.

The logic should be simple, intercept the creation process, detect if the item being added to the doclib is really a folder, and cancel the event.
But the reality, the ItemAdding event wouldn’t let me do the detection process.

So, a brutal method was then picked πŸ™‚
I resorted to the ItemAdded thing, and unleashed this evil;

public override void ItemAdded(SPItemEventProperties properties) { if (properties.ListItem.ContentType.Name == "Folder") { properties.ErrorMessage = "You can't create folders"; properties.ListItem.Delete(); } }

It worked. so stupidly enough, this will wipe the folder that you just created after detecting that the item you just added was a folder.

My two cents guys, who knows this might bail you out of jail too! πŸ™‚

Advertisements

4 Responses to "Preventing Folder Creation on MOSS 2007 Document Libraries"

[…] bookmarks tagged fun Preventing Folder Creation on MOSS 2007 Document L… saved by 5 others     finegirlx2xx bookmarked on 01/28/08 | […]

After a few hours of testing, I finally found the info to identify folders in the ItemAdding properties:
if (((string)properties.AfterProperties[“ContentType”]) == “Folder”)

Enjoy!

Thanks’ heaps mate, hopefully others will benefit from your findings! πŸ™‚

gbelzile’s solution works when the folder is created using the Doc Library menu interface. If, however, the user opens the doc library in Windows Explorer view the ContentType check does not catch it.

Add this and you will catch the Explorer view events as well:

if ((((string)properties.AfterProperties[“ContentType”]) == β€œFolder”) || properties.AfterUrl.ToString().Contains(“/”))

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: