Referencing Documents in the library by ID (and deleting the

Posted by Community Admin on 05-Aug-2018 18:00

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

All Replies

Posted by Community Admin on 11-Nov-2010 00:00

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?

Posted by Community Admin on 12-Nov-2010 00:00

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.

Posted by Community Admin on 14-Nov-2010 00:00

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

Posted by Community Admin on 14-Nov-2010 00:00

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.

This thread is closed