Delete Folders in Sharepoint Library folders using Object Model

using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb mySite = oSite.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
mySite.AllowUnsafeUpdates = true;
SPList list = mySite.Lists[“MeetingPointAttachments”];
for (int i = 0; i < list.RootFolder.SubFolders.Count; i++)
{
try
{
SPFolder folder = list.RootFolder.SubFolders[i];
if (folder.ItemCount == 0)
{
folder.Delete();
}
}
catch (Exception ex)
{
lblMessage.Text = ” Deleting Folders :” + ex.Message.ToString();
}
}
mySite.AllowUnsafeUpdates = false;
});
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami