More in this section

Forums / General Discussions / Download List Functionality

Download List Functionality

4 posts, 0 answered
  1. AFTR
    AFTR avatar
    22 posts
    Registered:
    05 Oct 2010
    21 Mar 2011
    Link to this post
    Hello,

    I am creating a custom user control to work with some of my document libraries.

    In my user control, I have code that will select documents from specific libraries based on the user's role.

    I need to create a user control with some of the same functionality of the Sitefinity document list control, but I also need some other abilities.

    In addition to the download link, each file needs to have a button to delete it. I also need to create an option to upload new files to my various libraries.

    Right now, on the page loads, I am creating rows and cells in an ASP:Table and populating them with the data I receive from this code....

    LibrariesManager libMan = LibrariesManager.GetManager();
     
    IEnumerable<Document> docs = libMan.GetDocumentLibraries().Where(dL => dL.Title == "My Library Name").First().Documents.Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);

    I am then using the following code to generate rows and cells within my table...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Sitefinity.Libraries.Model;

    ...

    TableRow fileRow = new TableRow();
    TableCell fileCell = new TableCell();
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
    fileCell.Text = "<a href=\"" + doc.MediaUrl + "\" target=\"_blank\"> Download</a>";
    fileRow.Cells.Add(fileCell);
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
    fileCell.Text = doc.Title;
    fileRow.Cells.Add(fileCell);
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
    fileCell.Text = doc.Extension;
    fileRow.Cells.Add(fileCell);
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
    double fileSize = (double)doc.TotalSize;
    if (fileSize < 1024)
    {
        fileCell.Text = doc.TotalSize.ToString("N02", System.Globalization.CultureInfo.InvariantCulture) + " B";
    }
    else if (fileSize < (1024 * 1024))
    {
        double computedSize = (fileSize / 1024);
        fileCell.Text = computedSize.ToString("N02", System.Globalization.CultureInfo.InvariantCulture) + " kB";
    }
    else
    {
        double computedSize = fileSize / 1024;
        computedSize = computedSize / 1024;
        fileCell.Text = computedSize.ToString("N02", System.Globalization.CultureInfo.InvariantCulture) + " mB";
    }
    fileRow.Cells.Add(fileCell);
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
    fileCell.Text = doc.PublicationDate.ToShortDateString() + " " + doc.PublicationDate.ToLongTimeString();
    fileRow.Cells.Add(fileCell);
     
    fileCell = new TableCell();
    fileCell = StyleCells(fileCell);
     
    LinkButton deleteLink = new LinkButton();
    deleteLink.ID = doc.Id.ToString();
    deleteLink.OnClientClick = "DeleteFileLink_Click";
    deleteLink.Text = "Delete";
     
    fileCell.Controls.Add(deleteLink);
    fileRow.Cells.Add(fileCell);
     
    fileRow.TableSection = TableRowSection.TableBody;
    FileTable.Rows.Add(fileRow);

    As you can see in the last cell, I create a link button, tie it to an event I have written in the code, and then add it to the cell controls. I am trying to set the ID of the file to the name of the control so I can have the ability to delete it.

    My problem is, the event is never firing.

    Also - as far as the upload goes, I am not sure how to go about uploading a file to the same library as above?
  2. AFTR
    AFTR avatar
    22 posts
    Registered:
    05 Oct 2010
    21 Mar 2011
    Link to this post
    Hello again. I have made some useful headway that I thought I would share.

    I have succeeded in figuring out how to upload documents! :)

    01.protected void UploadButton_Click(object sender, EventArgs e)
    02.        {
    03.            foreach (UploadedFile file in DocumentUpload.UploadedFiles)
    04.            {
    05.                var manager = new LibrariesManager();
    06.                Telerik.Sitefinity.Libraries.Model.Document uploadDocument = manager.CreateDocument();
    07.                uploadDocument.Parent = manager.GetDocumentLibraries().Where(dl => dl.Title == "My Library Name").First();
    08.                uploadDocument.Title = file.GetNameWithoutExtension();
    09.                uploadDocument.UrlName = file.GetNameWithoutExtension();
    10.                uploadDocument.Extension = file.GetExtension();
    11.                uploadDocument.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live;
    12.                manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Document>(uploadDocument);
    13.                manager.Upload(uploadDocument, file.InputStream, file.GetExtension());
    14.            }
    15. 
    16.            //refresh table
    17.            RefreshDocumentsTable();
    18.        }

    Now the only thing I really need to figure out is how to delete them?
  3. AFTR
    AFTR avatar
    22 posts
    Registered:
    05 Oct 2010
    21 Mar 2011
    Link to this post
    Hello again,

    I have encountered another problem. When I use the code above to upload a file to my documents library the following occurs.

    1. My ASP:Table that uses my custom code to select the documents in the first post works, and the documents appear in my list.
    2. Even though the documents are posted as "live" in the ContentLifecycleStatus, they do not appear in a regular "Document List" control I added through the sitefinity interface, nor do they appear in the Content -> Documents & Files section in the sitefinity backend. They only appear when they are selected with my custom code.

    Now. if I comment out line 11 (which sets the document as published), and leave all the remaining upload code unchanged, the uploaded document will not appear in my custom code, nor the "Document List" control, BUT it DOES appear in the Sitefinity backend Content -> Documents & Files as "draft". If i go there and publish it manually, it will correctly appear in all three places.

    I need the file to be published programatically when it is uploaded and be able to appear in all three locations.

    As it is now, I have some "mystery" files uploaded that only show up in my custom ASP:Table which I cannot see anywhere else.
  4. AFTR
    AFTR avatar
    22 posts
    Registered:
    05 Oct 2010
    22 Mar 2011
    Link to this post
    Hello again,

    Using the following code I was able to achieve publishing.

    01.protected void UploadButton_Click(object sender, EventArgs e)
    02.{
    03.    foreach (UploadedFile file in DocumentUpload.UploadedFiles)
    04.    {
    05.        var manager = new LibrariesManager();
    06.        Telerik.Sitefinity.Libraries.Model.Document uploadDocument = manager.CreateDocument();
    07.        uploadDocument.Parent = manager.GetDocumentLibraries().Where(dl => dl.Title == "My Library Name").First();
    08.        uploadDocument.Title = file.GetNameWithoutExtension();
    09.        uploadDocument.UrlName = file.GetNameWithoutExtension();
    10.        uploadDocument.Extension = file.GetExtension();
    11.        uploadDocument.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master;
    12.        manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Document>(uploadDocument);
    13.        manager.Upload(uploadDocument, file.InputStream, file.GetExtension());
    14. 
    15.        uploadDocument.ApprovalWorkflowState = "Published";
    16. 
    17.        manager.Publish(uploadDocument);
    18.        manager.SaveChanges();
    19.    }
    20. 
    21.    //refresh table
    22.    RefreshDocumentsTable();
    23.}

    However, my problem with the delete button still remains.
4 posts, 0 answered