How do I allow users to create folders but not delete folders?
-
16-10-2019 - |
Question
How can I allow users to create folders but not delete folders in SharePoint 2007?
Solution
Create a event reciever using the following code and then attach the event reciever to your document libraries.
private bool CanDeleteFolder(SPWeb web)
{
// check to see if the user is in the owners group
SPUser user = web.CurrentUser;
if (!user.IsSiteAdmin)
{
return false;
}
else
{
//admins can delete folder
return true;
}
}
public override void ItemDeleting(SPItemEventProperties properties)
{
try
{
if (properties.AfterProperties["ContentType"].ToString() == "Folder")
{
using (SPWeb web = properties.OpenWeb())
{
if (!CanDeleteFolder(web))
{
properties.Cancel = true;
properties.ErrorMessage = "Cannot add folder at this location. Please select a sub folder";
properties.Status = SPEventReceiverStatus.CancelWithError;
}
}
}
}
catch { }
}
OTHER TIPS
Or create a custom permission level that does not allow the delete option and apply that permission level to the library for the desired users. They'll be able to add/edit items but not delete. An event receiver seems like overkill for a simple permissions change.
Could use a custom event receiver to trap OnDeleting to see if the current item being deleted is a folder, and Cancel if so, presenting an error message whilst doing so.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange