Site structure
Version 3.x > Site structure > Create Multiple Folders View modes: 
User avatar
Certified Developer 8
Certified Developer 8
bryan-bit-wizards - 1/6/2009 4:39:30 PM
   
Create Multiple Folders
I need to create a large number of folders (200+) in my site. Is there a way to do this programmatically? Perhaps calling a stored procedure and passing it the name/etc? I checked the database / API references but wasn't able to easily identify anything that would accommodate this.

Thank you for any help you can provide.

-Bryan

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/7/2009 1:15:37 AM
   
RE:Create Multiple Folders
Hi Bryan,

Here you can find API examples how to manage documents - http://www.kentico.com/docs/devguide/creating_documents.htm

If you are using workflow, please see these examples - http://www.kentico.com/docs/devguide/a_complete_example.htm.

Best Regards,
Juraj Ondrus

User avatar
Certified Developer 8
Certified Developer 8
bryan-bit-wizards - 1/7/2009 2:21:49 PM
   
RE:Create Multiple Folders
Thank you for your help. Using the documentation, I was able to programmatically add the foider.

This code will create the main folder and 2 subfolders for every record returned by the query.

Code:


string strName = "";
GeneralConnection cn = new GeneralConnection();
DataSet ds = null;
object[,] parameters = new object[1, 3];
parameters[0, 0] = "";
parameters[0, 1] = "";
ds = cn.ExecuteQuery("[Query Name]", parameters);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
strName = dr[3].ToString();

// create tree provider instance
CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

// get parent node for new document
CMS.TreeEngine.TreeNode parent = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSite.SiteName, "/Images/[Folder Name]", "en-us");

// create a new tree node
CMS.TreeEngine.TreeNode node = new CMS.TreeEngine.TreeNode("CMS.Folder", provider);

if (parent != null)
{
//Create the main folder
// set document properties
node.NodeName = strName;
node.NodeAlias = strName;
node.SetValue("DocumentName", strName);
node.SetValue("DocumentCulture", "en-us");
// create New document
node.Insert(parent.NodeID);

//Create Subfolders
//Index
// create a new tree node
CMS.TreeEngine.TreeNode nodeIndex = new CMS.TreeEngine.TreeNode("CMS.Folder", provider);
// set document properties
nodeIndex.NodeName = "Index";
nodeIndex.NodeAlias = "Index";
nodeIndex.SetValue("DocumentName", "Index");
nodeIndex.SetValue("DocumentCulture", "en-us");
// create New document
nodeIndex.Insert(node.NodeID);

//Images
// create a new tree node
CMS.TreeEngine.TreeNode nodeImages = new CMS.TreeEngine.TreeNode("CMS.Folder", provider);
// set document properties
nodeImages.NodeName = "Images";
nodeImages.NodeAlias = "Images";
nodeImages.SetValue("DocumentName", "Images");
nodeImages.SetValue("DocumentCulture", "en-us");
// create New document
nodeImages.Insert(node.NodeID);
}
}
}

-Bryan