Site structure
Version 5.x > Site structure > Copy Multiple Items View modes: 
User avatar
Member
Member
nfalk - 12/8/2010 11:19:04 AM
   
Copy Multiple Items
Is there a way to copy all the child nodes from one node to the next?

For example, if this is my site structure:

- Parent Node 1
- - Child Node A
- - Child Node B
- - Child Node C
- - Child Node D
- Parent Node 2
- Parent Node 3

Can I copy all Parent Node 1's Children to Parent Node 2 without copying each child one-by-one?

So the structure would look like:

- Parent Node 1
- - Child Node A
- - Child Node B
- - Child Node C
- - Child Node D
- Parent Node 2
- - Child Node A
- - Child Node B
- - Child Node C
- - Child Node D
- Parent Node 3

I know you can hold ctrl and drag to copy, but when you have 20+ Nodes under a parent this doesn't work so well.

Thank you in advance.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 12/8/2010 1:58:42 PM
   
RE:Copy Multiple Items
Hi,

you can use API to copy multiple nodes. You can find an inspiration in the following example.


CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);
DataSet nodesDataSet = provider.SelectNodes(CMS.CMSHelper.CMSContext.CurrentSiteName, "/From-Directory/%", "en-us", true, "classname", "");
CMS.TreeEngine.TreeNode target = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/Products/Notebooks", "en-us");

if (!CMS.GlobalHelper.DataHelper.DataSourceIsEmpty(nodesDataSet))
{
foreach (DataRow row in nodesDataSet.Tables[0].Rows)
{
CMS.TreeEngine.TreeNode node = new CMS.TreeEngine.TreeNode(row, "classname", provider);
if ((target != null) && (node != null))
{

// copy the document under the specified target,
// the third parameter says that document`s child nodes (if any) will not be copied

provider.CopyNode(node, target.NodeID, false);

}
}
}


Best regards,
Ivana Tomanickova

User avatar
Member
Member
nfalk - 12/9/2010 8:51:31 AM
   
RE:Copy Multiple Items
Thanks Ivana, I didn't even think about doing that.

I ended up creating something similar to your suggestion and it is saving me a lot of time.

User avatar
Member
Member
nfalk - 12/15/2010 1:12:45 PM
   
RE:Copy Multiple Items
I just found another way to do this and I'll document it here in case anyone else is interested.

Basically, when you click on a node... Change to the "List" view mode. It will then list all child documents for that node. Check the documents you want to move and at the bottom of the list page there is a drop down where you can Link or Copy the selected documents.

User avatar
Member
Member
adegiamb - 8/15/2011 2:54:53 PM
   
RE:Copy Multiple Items
I am trying to use the above code but its not working correctly. When I run the code the child documents from parent 1 are getting copied to Parent 2 but the node order is not get respected. How can I copy parent 1 child documents to parent 2 while respecting the current node order in Parent 1.

Parent 1
- Child 1
- Child 2
- Child 3
Parent 2 - after copy
- Child 3
- Child 1
- Child 2

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/16/2011 3:14:42 AM
   
RE:Copy Multiple Items
Hi,

could you please try to use following code:


DataSet dsAllNodes = null;
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
tree.MergeResults = true;
//disable automatic ordering
tree.UseAutomaticOrdering = false;
dsAllNodes = tree.SelectNodes("CorporateSite", "/Products/Laptops/%", CMSContext.CurrentUser.PreferredCultureCode, true, "", "", "NodeLevel, NodeOrder");
TreeNode target = tree.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/Products/Notebooks", "en-us");

foreach (DataRow row in dsAllNodes.Tables[0].Rows)
{
CMS.TreeEngine.TreeNode node = new CMS.TreeEngine.TreeNode(row, "cms.laptop", tree);
if ((target != null) && (node != null))
{
// copy the document under the specified target,
// the third parameter says that document`s child nodes (if any) will not be copied
tree.CopyNode(node, target.NodeID, false);
}
}


Setting property UseAutomaticOrdering to false and selecting nodes in order "NodeLevel, NodeOrder" should help.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
adegiamb - 8/22/2011 2:26:21 PM
   
RE:Copy Multiple Items
Ok that fixed the sorting issue but now I noticed now that my Thumbnail field that is of type File value is not getting copied.

Do I need to do anything special for File fields?

thanks

ADG

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/23/2011 5:52:48 AM
   
RE:Copy Multiple Items
Hi,

that is strange. I tested the behavior with above code and with custom field defined as:

Attribute type: File
Field type: Upload file || Direct uploader // tested both options

and the image was copied correctly. I mean a reference to the image because in the field is stored its GUID.

Just for confirmation - your Thumbnail field is a custom field defined in a document type which is copied.

Could you please check if the GUID is copied in your database? Are you using the same code? Aren't you selecting only custom fields?

Thank you in advance for information.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
adegiamb - 8/23/2011 6:32:20 AM
   
RE:Copy Multiple Items
Below is the code I am using and yes i am including custom field values.


var nodesDataSet = treeProvider.SelectNodes(CMSContext.CurrentSiteName,
searchAliasPath, CMSContext.CurrentUser.PreferredCultureCode, true,
ClassName, string.Empty, "NodeLevel, NodeOrder",1,false);



if (!CMS.GlobalHelper.DataHelper.DataSourceIsEmpty(nodesDataSet))
{
foreach (DataRow row in nodesDataSet.Tables[0].Rows)
{
var node = new TreeNode(row, ClassName, treeProvider);
if ((newContainerNode != null) && (node != null))
{

var newNode = treeProvider.CopyNode(node, newContainerNode.NodeID, true);


}
}
}


User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/23/2011 8:53:10 AM
   
RE:Copy Multiple Items
Hi,

could you please replace:

var newNode = treeProvider.CopyNode(node, newContainerNode.NodeID, true);

with:

CMS.WorkflowEngine.DocumentHelper.CopyDocument(node, targetNodeID, true, tree);

The original code really copied only reference on the image. Using DocumentHelper class a new instance of image should be created (with a new GUID).

Best regards,
Ivana Tomanickova