1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Cannot delete document programmatically

Cannot delete document programmatically

4 posts, 0 answered
  1. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    03 Jan 2012
    Link to this post
    Hi,

    I can't manage to delete a document programmatically. I get no exception, it just does nothing.

    I tried many different things, including:

    var doc = LibrariesManager.GetManager().GetDocument(id);
    LibrariesManager.GetManager().DeleteDocument(doc);
    LibrariesManager.GetManager().SaveChanges();

    As well as:

    var doc = LibrariesManager.GetManager().GetDocument(id);
    LibrariesManager.GetManager().DeleteItem(doc);
    LibrariesManager.GetManager().SaveChanges();

    Or:

    App.WorkWith().Documents().Publihed().Where(d => d.Id == id).Delete();

    Nothing works.
    And I can retrieve the document fine, so the "doc" or "id" variables have the right content.

    This is with Sitefinity 4.3 (and we're not gonna update anytime soon).
  2. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    03 Jan 2012
    Link to this post
    Hi Thomas,

    The first bit of code is incorrect - you get a new manager for every call, which is not right. You need to get a single instance of a manager and operate only with it. So the code should be:

    var manager = LibrariesManager.GetManager();
    var doc = manager.GetDocument(id);
    manager.DeleteDocument(doc);
    manager.SaveChanges();

    All the best,
    Lubomir Velkov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    03 Jan 2012
    Link to this post
    Hi Lubomir,

    Actually, that's already what I do in my code. I changed it here because I thought that would simplify things, removing one line. I assumed it'd be the same instance every time.
    So yeah, that's not the issue.
  4. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    03 Jan 2012
    Link to this post
    Hi Thomas,

    I uploaded one document and tried the following code and it deleted it. Are you sure the "id" parameter that you pass to GetDocument is valid? It must the "id" of the Live version of the document.

    Regards,
    Lubomir Velkov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
4 posts, 0 answered