Showing posts with label subfolder. Show all posts
Showing posts with label subfolder. Show all posts

Saturday, May 24, 2014

Creating folder and sub folder programmatically in SharePoint

public static void createFolderSubFolder()
        {
            string libraryName = "Test";
            string folderName = "Folder";
            string siteUrl = "http://myserver03:8989/abc";
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
              
                    using (SPSite osite = newSPSite("http://myserver03:8989/abc"))
                    {
                        using (SPWeb oweb = osite.OpenWeb())
                        {
                            SPFolderCollection folderCollection = oweb.Folders;
                            string newFolderUrl = siteUrl + "/" + libraryName + "/" + folderName;
 
                            SPFolder newFolder = folderCollection.Add(newFolderUrl);
 
                            Console.WriteLine("Folder created");
 
                            string secondFolderName = "subFolder";
                            string newSubFolderUrl = newFolderUrl + "/" + secondFolderName + "/";
 
                            SPFolder newSubFolder = folderCollection.Add(newSubFolderUrl);
                            Console.WriteLine("SubFolder created");
                            Console.ReadLine();
                        }
                    }
                });
 
            }
 
            catch (Exception ex)
            {
                throw ex;
            }
 
        }





Please note : Please check out my blog on how to create a library,content type,columns,subsite programmatically.