Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

De-linking Linked Documents in Kentico

Modified: 2014/06/05 09:52 by jim - Categorized as: Evident, Kentico
For the website www.vierstroom.nl we built the following functionality: when a news item is added, a corresponding Linked document can be added to a calendar or vice versa. When the editors copy the original item, nothing out of the ordinary is needed. But when editors copy the Linked document, they effectively create a new linked document, linking to the original node. When they edit this last copy, they don't edit that copy, but they edit the original node. They didn't understand what was happening. We could have told the editors they were copying the wrong node, but since they can do it, it looks like we had to fix it. From the Kentico API there is no way to promote the linked document to a Document with a life of it's own.

We customized the Document.Copy.After event, where, when the right conditions are met:

private void BreakLinkedDocument(TreeProvider treeProvider, TreeNode copiedDoc) {
// to unlink a linked document you have to create new one, and delete the newly created linked document.
// linked documents are only CMS_Tree records, only nodes. All else is taken from the NodeLinkedNodeID node-document and class
   int originalID = copiedDoc.GetIntegerValue("NodeLinkedNodeID", 0);
   if (originalID != 0) {
   // get parent
       TreeNode originalDocument = DocumentHelper.GetDocument(originalID, copiedDoc.DocumentCulture, treeProvider);
       // remove trigger field value that will create another linked document.
       originalDocument.SetValue("InsertTwice", false);
       DocumentHelper.CopyDocument(originalDocument, copiedDoc.NodeParentID, false, treeProvider);
       // Delete the document
      DocumentHelper.DeleteDocument(copiedDoc, treeProvider, false, false, false);
   }
 }

 © Evident Interactive BV