Last active
June 20, 2018 06:07
-
-
Save sherazlodhi/e1bc84a504ee5072ea7174373f1f41b4 to your computer and use it in GitHub Desktop.
Delete All Empty folders sharepoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List < SPFolder > emptyDirectories = new List < SPFolder > (); | |
public void DeleteEmpty() { | |
string spsiteURL = ""; | |
string listName = ""; | |
using(SPSite site = new SPSite(spsiteURL) { | |
using(SPWeb web = site.OpenWeb()) | |
if (web != null) | |
{ | |
do { | |
foreach(var item in emptyDirectories) { | |
try { | |
item.ParentWeb.AllowUnsafeUpdates = true; | |
item.Delete(); | |
item.ParentWeb.AllowUnsafeUpdates = false; | |
} catch { | |
} | |
} | |
emptyDirectories.Clear(); | |
SPList list = web.Lists[listName]; | |
ProcessDirectory(list.RootFolder.SubFolders); | |
} while (emptyDirectories.Count > 0); | |
} | |
} | |
} | |
private void ProcessDirectory(SPFolderCollection subFolders) { | |
foreach(SPFolder folder in subFolders) { | |
if (folder.Name == "Forms") { | |
continue; | |
} | |
ProcessDirectory(folder.SubFolders); | |
if (folder.Files.Count == 0 && folder.SubFolders.Count = 0) | |
{ | |
emptyDirectories.Add(folder); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment