Monday, June 13, 2011

How to prevent users from renaming folders?

Scenario: Need to prevent users from modifying the folder name in a document library
Solution: You need to add an item updating event to the library.

Here is some sample code:

try
{

if (properties.ListTitle == "YOUR LIBR")
{
//Only perform this check if the user is not a site collection admin.
if (!properties.Web.CurrentUser.IsSiteAdmin)
{
if (properties.ListItem.FileSystemObjectType == SPFileSystemObjectType.Folder)
{
properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = properties.WebUrl.ToString() + "/_Layouts/...../ErrorPage.aspx?errormessage=" + "Cannot rename the folder";
}
}
}

}
catch (Exception e1)
{
properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = properties.WebUrl.ToString() + "/_Layouts/..../ErrorPage.aspx?errormessage=" + e1.Message;
}
finally
{
if (!properties.Cancel)
{
base.ItemUpdating(properties);
}
}

}

A few notes:
- If you don't specify the redirect URL, SP will show a nasty dialog box like ASP.NET crashed or something with your message.This may not be liked by the business.

- You should always check for the type of object you want to prevent from getting updated.

- If there is a typo or you missed "/" in your RedirectURL, SP will display the nasty box and ignore going to ur Application page.

No comments:

Post a Comment