ASPX templates
Version 5.x > ASPX templates > Linking to existing content View modes: 
User avatar
Member
Member
lwhittemore-emh - 6/7/2011 12:54:39 PM
   
Linking to existing content
I have a document type that is used to create banners with links. I have also created a folder with these banners in them in the menu or tree on my site.

When I go to an existing page and add a new linked document to it and click the include child box all seems well. A folder shows under my page with the existing documents. however, when I update the original folder by adding new documents into they do not carry over to the linked folder.

is there a way so that it will always keep new documents in the folder linked?

Thanks

Hope this made sense.

User avatar
Member
Member
kentico_michal - 6/8/2011 3:10:16 AM
   
RE:Linking to existing content
Hello,

The node in the content tree is identified as linked, if it has the property NodeLinkedNodeID defined. If it's null, node is not a linked document.

So, you can use a custom TreeNode handler and check whether the new document is the banner added to the original folder and if so, find all documents in the content tree that has NodeLinkedNodeID set to the NodeID of the parent folder. Than you can use TreeNode.InsertAsLink method to create a new linked document.

So, in general the code could look like the following one:



public override void OnAfterInsert(object treeNodeObj, int parentNodeId, object tree)
{
// INSERT YOUR CUSTOM AFTER-INSERT CODE

UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(ui);
provider.MergeResults = true;
TreeNode newDoc = treeNodeObj as TreeNode;

// check the parent document
if (newDoc.NodeAliasPath.StartsWith("/Banners/") && provider != null)
{
TreeNode parentDoc = newDoc.Parent;

// get all linked document of the parent document
DataSet ds = provider.SelectNodes("SiteName", "/%", "en-us", true, "", "NodeLinkedNodeID = " + parentDoc.NodeID.ToString());

if (ds != null)
{
TreeNode linkedDoc = null;
foreach (DataRow row in ds.Tables[0].Rows)
{

// create linked document
linkedDoc = provider.SelectSingleNode( ValidationHelper.GetInteger(row["NodeID"], 0));

if (linkedDoc != null)
{
newDoc.InsertAsLink(linkedDoc.NodeID);
}

}
}

}
}


If you need more information about creating a linked document using API, I would like to point you to the following section of Developer's guide and to our API reference: API reference, Create linked document

Best regards,
Michal Legen