Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to create dynamic folders and subfolders in a document library when any new folder created in the same library.
Also I want to repeat the same for number of document libraries which may present in another subsites.

I have a made a sample code to create folder whenever any user add any new folder. It is creating the folders but at the time of debugging it is throwing array out of bounds error. Though it is creating folders as per desired.

public class DynamicFolderCreationEventReciever : SPItemEventReceiver
    {
        private string[] folders = new string[] { "One", "Two", "Three" };
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           try
           {
               using (SPSite objSPSiteCollection = new SPSite(SPContext.Current.Web.Url))
               {
                   using (SPWeb objSPWeb = objSPSiteCollection.RootWeb)
                   {

                      // base.ItemAdded(properties);
                       SPListItem item = properties.ListItem;
                       //SPDocumentLibrary objDocLib = (SPDocumentLibrary)objSPWeb.Lists[properties.ListId];   
                       SPDocumentLibrary obj=objSPWeb.Lists["CustomDocLibrary"] as SPDocumentLibrary;
                       if (item.FileSystemObjectType== SPFileSystemObjectType.Folder )
                       {
                           String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
                           SPFolder libFolder = obj.RootFolder.SubFolders[properties.ListItem.Name];

                           string newFolderUrl = (objSPWeb.Url + "/" + libFolder.ToString());

                           for (int i = 0; i < folders.Length; i++)
                           {
                               SPListItem newFolder = obj.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, folders[i].ToString());
                               newFolder.Update();
                           }
                       }
                       else
                       {

                       }


                   }
               }
           }
           catch (SPException ex)
           {
               throw ex;
           }
       }

    }
C#



I don't know why that error coming while debugging. And how can I create subfolders.



Thanks
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900