How to prevent delete parent if parent have childs in pages

duong nguyen asked on March 19, 2019 13:15

Hi every one,

I need some help from everyone.

I have pages in kentico, I want to prevent deleting pages if they have childs

  • Category
    • Sub Category

At the current, kentico can delete. But I want can not delete Category because it have Sub Category.

How I can do It. Thank you so much. Image Text

Correct Answer

Roman Hutnyk answered on March 19, 2019 13:28

EventArgs parameter that your handler method receives contains TreeNode you're about to delete.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on March 19, 2019 13:20 (last edited on March 19, 2019 13:20)

You could hook into DocumentEvents Delete Before event, do a check for child pages and cancel deleting of the page.

Probably this is not the best option as it won't show any alert/message on the UI, but it will prevent deleting of a page.

0 votesVote for this answer Mark as a Correct answer

duong nguyen answered on March 19, 2019 13:25

Thank you Roman Hutnyk, I did it but getting some issues

When I select any pages to delete.

  • select Category to delete => it always get last NodeID (get App NodeID)
  • select Sub Category to delete => It always still get last NodeID (get App NodeID)

I want to get the current NodeID but I can't . Ex: I want to get NodeID of Category when I click delete Category.

How can I do it.

Thank you for your help again.

0 votesVote for this answer Mark as a Correct answer

duong nguyen answered on March 19, 2019 13:32 (last edited on March 19, 2019 13:35)

Thank you Roman Hutnyk, but I can not find out the TreeNode. Could you please provide me or tell me the method name.

Thank you again

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 19, 2019 13:37

See the example of document insert event: https://docs.kentico.com/k12/custom-development/handling-global-events

Delete handler works the same way.

 private void Document_Insert_After(object sender, DocumentEventArgs e)
{
    // Add custom actions here
}

DocumentEventArgs e parameter that is passed to the method contains reference to the document you're making action with:

e.Node

Hope that helps.

0 votesVote for this answer Mark as a Correct answer

duong nguyen answered on March 19, 2019 13:46 (last edited on March 19, 2019 13:47)

Thank youRoman Hutnyk

you miss understand me

  • category : e.Node.NodeID = 1
  • sub category: e.Node.NodeID = 2
  • app: e.Node.NodeID = 3

When I click delete on any pages: it always show e.Node.NodeID = 3

How can I get the nodeID of selected pages. In Document_Update_After case It get the right NodeID. Document_Delete case is not get right current NodeID

Thank you so much

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 19, 2019 14:44

that's interesting... probably Kentico starts to remove pages starting from the children, so your event handler has to be called for three times: app, sub category, category.

Do you know the pages you don't want to be removed? - if so, you could just setup security to deny deleting.

0 votesVote for this answer Mark as a Correct answer

duong nguyen answered on March 19, 2019 15:02

I'm very appreicate about your help. Yes, it call three time so that I can not handle all case.

Actually, If Category have any Sub Category, should not be deleted If Sub Category have any App, should not be deleted.

How I can setup security to deny deleting.

Thank you so much again

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 19, 2019 15:14

Not sure if you can setup conditional security... You can either apply it or not

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.