Kentico TreeNode Delete method not deleting dependencies
Asked Answered
T

1

5

I have the following block of code that retrieves a document node in kentico and deletes it. It does delete the kentico node, but not the underlying document type which stays in the datase. Help?!

CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);
CMS.TreeEngine.TreeNode image = provider.SelectSingleNode(new Guid(imageID), "en-US", CMS.CMSHelper.CMSContext.CurrentSite.SiteName);

if (image != null)
{
    CMS.TreeEngine.TreeNode school = provider.SelectSingleNode(image.Parent.NodeID, "en-US", true, true);
    if (school != null)
    {
        string CMSUserID = school.GetValue("CMSUserID").ToString();
        if (CMSUserID == ui.UserID.ToString())
        {
            image.Delete(false);                                        
        }
    }
}
Theophilus answered 19/7, 2011 at 1:0 Comment(0)
K
8

You need to use the DeleteDocument method from the CMS.WorkflowEngine namespace. It ensures that all dependent objects are deleted.

DocumentHelper.DeleteDocument(image, provider, true, true, true);

Kan answered 19/7, 2011 at 1:1 Comment(2)
Thanks that worked. Strange that my original code doesn't work, I don't use any Kentico workflow though?Theophilus
Using the workflow method ensures that ALL dependent objects are deleted. I concur that the Treenode.Delete() method should work as you expected, especially as you are using the overloaded method with preserve data set to 'false'...Kan

© 2022 - 2024 — McMap. All rights reserved.