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);                                        
        }
    }
}
rocky
  • 7,506
  • 3
  • 33
  • 48
Grimboify
  • 218
  • 1
  • 11

1 Answers1

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);

Ben E G
  • 971
  • 8
  • 16
  • Thanks that worked. Strange that my original code doesn't work, I don't use any Kentico workflow though? – Grimboify Jul 19 '11 at 01:05
  • 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'... – Ben E G Jul 19 '11 at 01:07