Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Create Folders and subfolders programmatically using C#

$
0
0

Hi

How do I create a folder and its subfolders programmaticallyusing C#. The folder\sufolders names are coming from a database column called foldernames. The value  stored in the column are below: 

 Folder1\Folder2\Folder3\Folder4

I need to read from the column and create folders and subfolders based on the value returned by the column.

For example,Folder1 should be the parent folder and have a subfolder calledFolder2...and Folder2 should have a subfolder calledFolder3 and Folder3 should have a subfolder calledFolder4.

I am using the below code

     static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection(@"Persist Security Info=False;User ID=doe;Password=mypassword;Initial Catalog=mydatabse;Data Source=myserver;");
            SqlDataAdapter da = new SqlDataAdapter("EXEC [myproc]", con);
            DataTable tb = new DataTable("Table1");     

  da.Fill(tb);

SPSite site = new SPSite("http://mysharept01:50000/");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["Folder"];



            foreach (DataRow dr in tb.Rows)
            {
                Console.WriteLine(dr["Folder"].ToString());
                SPListItem newfolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dr["Folder"].ToString());
                newfolder.Update();

                SPListItem subfolder = list.Items.Add(newfolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dr["Folder"].ToString());
                subfolder.Update();

      }

}

}

}

Your assistance will be highly appreciated as I am currently a newbie to C#.


e.e




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>