Recursive Folder Creation method for SharePoint


  public static class SPFolderExtensions  
   {  
     /// <summary>  
     /// Ensure SPFolder  
     /// </summary>  
     /// <param name="web"></param>  
     /// <param name="listTitle"></param>  
     /// <param name="folderUrl"></param>  
     /// <returns></returns>  
     public static SPFolder CreateFolder(this SPWeb web, string listTitle, string folderUrl)  
     {  
       if (string.IsNullOrEmpty(folderUrl))  
       {  
         throw new ArgumentNullException("folderUrl");  
       }  
       var list = web.Lists.TryGetList(listTitle);  
       return CreateFolderInternal(list, list.RootFolder, folderUrl);  
     }  
     /// <summary>  
     /// Creates the folder internal.  
     /// </summary>  
     /// <param name="list">The list.</param>  
     /// <param name="parentFolder">The parent folder.</param>  
     /// <param name="folderUrl">The folder URL.</param>  
     /// <returns></returns>  
     private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)  
     {  
       var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);  
       var folderName = folderNames[0];  
       var curFolder = parentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => String.Compare(f.Name, folderName, StringComparison.OrdinalIgnoreCase) == 0);  
       if (curFolder == null)  
       {  
         var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,  
                         folderName);  
         folderItem.SystemUpdate();  
         curFolder = folderItem.Folder;  
       }  
       if (folderNames.Length > 1)  
       {  
         var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);  
         return CreateFolderInternal(list, curFolder, subFolderUrl);  
       }  
       return curFolder;  
     }  
   }  

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object