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

Forums / Sitefinity SDK / Referencing Documents in the library by ID (and deleting them)

Referencing Documents in the library by ID (and deleting them)

4 posts, 0 answered
  1. David
    David avatar
    13 posts
    Registered:
    23 Jun 2010
    11 Nov 2010
    Link to this post
    Hello Telerik staff,

    I ran into an issue, when I tried to delete a document from the Sitefinity Library in the SDK programmatically. I store my documents in the library using the following code (as suggested in the JobsModule example):

    using (var sf = Telerik.Sitefinity.App.WorkWith())
    {
      var doc = sf.Document().CreateNew().Do(item =>
      {
        item.Parent = sf.DocumentLibraries().Where(l => l.Title == "Konstruktionen").Get().FirstOrDefault();
        item.Title = uploadedFile.GetName();
      }).CheckOut();
      Document document = doc.UploadContent(uploadedFile.InputStream, uploadedFile.GetExtension()).CheckInAndPublish().Get();
        
      //Store the id in a variable of type Guid
      guid = document.Id;
    }

    The above code works great. Later on, I try to delete the document from the library using the following code (with the guid mentioned above):

    using (var sf = Telerik.Sitefinity.App.WorkWith())
    {
      sf.Documents().Where(c => c.Id == guid).Delete().SaveChanges();
    }

    The problem is now that after executing this line, the document isn't deleted permanently from the library. It is just unpublished and it's status is set to "draft".

    What am I doing wrong here? Is the Delete-command not doing what it should do or is referencing the items in the library by Id not possible?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    12 Nov 2010
    Link to this post
    Hello David,

    Thank you for getting back to us.

    Can you please let us know which version of Sitefinity you are using? I have tried this with Sitefinity 4.0 Beta 2 Build 685 and the item is properly deleted. Bellow is the code I used:
    App.WorkWith().Documents().Where(d => d.Title == "Document Title").Delete().SaveChanges();

    If you know the item ID you can also try this:
    App.WorkWith().Document(guid).Delete().SaveChanges();


    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. David
    David avatar
    13 posts
    Registered:
    23 Jun 2010
    14 Nov 2010
    Link to this post
    Hi Radoslav!

    Thanks for your reply. I'm using the version of Sitefinity that ships with the SDK (Beta 2, build 685). I just tried the other line of code you suggested (App.WorkWith().Document(guid).Delete().SaveChanges();), but that didn't help either. The document is just unpublished and it's status is set to draft.

    I also tried to reference a document by title and then delete it:
    App.WorkWith().Documents().Where(c => c.Title == "document.pdf").Delete().SaveChanges();

    That works well and deletes the document from the library. The problem is that I might have multiple documents with the same title in my library so I rather want to reference them by ID (which doesn't work).

    Best regards,
    David
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    14 Nov 2010
    Link to this post
    Hello David,

    There are changes in the Fluent API and this issue cannot be reproduced by us in the internal builds. I prepared a sample demo for you.

    control template

    <asp:Button runat="server" ID="Trigger" Text="click to execute" />


    code behind

    void Trigger_Click(object sender, EventArgs e)
          {
             
              var document = App.WorkWith().Documents().Get().FirstOrDefault();
              if (document != null && document.Id != Guid.Empty)
              {
                  var itemID = document.Id;
                  var alertText = @"<script type='text/javascript'>" + "alert('" + itemID + "');" + "</script>";
                  this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "itm1", alertText);
                  App.WorkWith().Documents().Where(doc => doc.Id == itemID).Delete().SaveChanges();
              }
              else
              {
                  var alertText = @"<script type='text/javascript'>" + "alert(\" There is no items in the library\");" + "</script>";
                  this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "itm1", alertText);
              }
          }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
4 posts, 0 answered