Kentico CMS 6.0 Developer's Guide

Managing relationships

Managing relationships

Previous topic Next topic Mail us feedback on this topic!  

Managing relationships

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a relationship between two documents using the relationship name created by the first code example on the previous page.

 

private bool CreateRelationship()

{

  // Get the relationship name

  RelationshipNameInfo relationshipName = RelationshipNameInfoProvider.GetRelationshipNameInfo("MyNewRelationshipName");

  if (relationshipName != null)

   {

      // Get the tree structure

      TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

      // Get documents for relationship (the Root document is used for both in this example)

      TreeNode root = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/", null, true);

 

      // Create the relationship between documents

      RelationshipProvider.AddRelationship(root.NodeID, root.NodeID, relationshipName.RelationshipNameId);

 

      return true;

   }

 

  return false;

}

 

The following example deletes the relationship created by the code example above.

 

private bool DeleteRelationship()

{

  // Get the relationship name

  RelationshipNameInfo relationshipName = RelationshipNameInfoProvider.GetRelationshipNameInfo("MyNewRelationshipName");

  if (relationshipName != null)

   {

      // Get the tree structure

      TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

      // Get documents which are in relationship (the Root document is used for both in this example)

      TreeNode root = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/", null, true);

 

      // Delete the relationship between documents

      RelationshipProvider.RemoveRelationship(root.NodeID, root.NodeID, relationshipName.RelationshipNameId);

 

      return true;

   }

 

  return false;

}