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

Forums / Developing with Sitefinity / Sortable Columns in Document Library

Sortable Columns in Document Library

23 posts, 0 answered
  1. Erik
    Erik avatar
    34 posts
    Registered:
    06 Dec 2007
    21 May 2008
    Link to this post
    Hello,

    I'm using the Document library DownloadList and am so far liking it.  There are a few things I'm wondering about that may or may not be complicated tweaks.

    First I would like to know how to add custom attributes to the libraries for extra metadata that can be displayed as columns in the DownloadList

    Second I would like to make these columns sortable via clicking to sort ascending or descending, much like the ability to do so in the backend CMS view of the libraries.

    If someone could give me an explanation for how to do these 2 simple things, this will greatly improve and make this library functionality like twice as useful

    Thanks!
    Erik
  2. Erik
    Erik avatar
    34 posts
    Registered:
    06 Dec 2007
    22 May 2008
    Link to this post
    I figured out how to add additional metafields.  That was the easy part.  I've also updated the admin templates for editing the "Date" field that I created.  Basically I have lots of documents that need to be sorted by the Date they were published (or created on the file system) as opposed to the date they were uploaded to Sitefinity's library.

    This custom Date field was created because it looks like the default behavior of the library upload function does not capture that metadata automatically.  So, until that functionality can be accomplished (which I encourage be done) I am left with doing this manually.

    The hurdle I am trying to get over now is displaying this custom Date metafield in my DownloadList control.  I need to know which template file to modify to add the appropriate checkbox for this column and I also need to know the proper sort expression to get the table to sort on that column.  I assume since I named my metafield "Date" that the expression would be "Date DESC" but that doesn't seem to work.  Here is the code for my metafield in the web.config:

    <add key="Libraries.Date" valueType="DateTime" visible="True" searchable="True" sortable="True" defaultValue="#Now" /> 

    Thanks,
    Erik
  3. Rebecca
    Rebecca avatar
    536 posts
    Registered:
    24 Sep 2012
    23 May 2008
    Link to this post

    Hi Erik,

    Here is how we achieved this:

    1. In the <metafields> web.config section added this key:

    <add key="Libraries.DateCreated" valueType="DateTime" visible="True" searchable="True" sortable="True" defaultValue="" /> 

    2. In the <libraryInfo> web.config section added DateCreated to the metakeys for the document library type:

          <libraryInfo> 
            <add name="Image" title="Image Library" defaultExtenstions=".png, .jpg, .jpeg, .gif" metaKeys="Author, Date, Description, AlternateText, Extension, Name, Height, Width, Size, Category">  
            </add> 
            <add name="Document" title="Document Library" defaultExtenstions=".doc, .docx, .rtf, .txt, .pdf, .ppt, .pptx, .html, .xls, .xlsx" metaKeys="Author, DateCreated, Description, Extension, Name, Size, Category">  
            </add> 
            <add name="Custom" title="Custom Library" defaultExtenstions=".*" metaKeys="Author, Description, Extension, Name, Size, Category">  
            </add> 
          </libraryInfo> 

    3. To show the filed in Sitefinity admin section, modified the ~/Sitefinity/Admin/ControlTemplates/Libraries/ControlPanelEdit.ascx like this:

    <sfGCn:ContentMetaFields ID="editMetaFields" runat="server">  
                        <ItemTemplate>  
                            <div>  
                                <asp:Label ID="Label8" runat="server" AssociatedControlID="DateCreated" Text="Date Created"></asp:Label>  
                                <telerik:raddatepicker ID="DateCreated" runat="server" >  
                                        </telerik:raddatepicker>                                      
                            </div>  
                            <h3>  
                                <asp:Literal ID="Literal11" runat="server" Text="<%$Resources:Details %>"></asp:Literal></h3>  
                            <fieldset class="set">  
                                <ol class="setIn">  
                                    <li>  
                                        <asp:Label ID="Label7" AssociatedControlID="Author" runat="server" Text="<%$Resources:Author %>"></asp:Label>  
                                        <asp:TextBox runat="server" ID="Author"></asp:TextBox>  
                                    </li> 

    4. To show the column in the Download List control (in table view), modified the ~/Sitefinity/ControlTemplates/Libraries/DownloadListTableModeTemplate.ascx like this:

    <HeaderTemplate>  
            <table class="sf_libraryGrid">  
                <thead>  
                <tr>  
                    <th scope="col" id="TitleWrap" runat="server"><asp:Literal ID="Literal1" runat="server" Text="<%$Resources:Title %>"></asp:Literal></th>  
                    <th scope="col" id="DateCreatedWrap" runat="server"><asp:Literal ID="Literal7" runat="server" Text="Date Created"></asp:Literal></th> 
    <ItemTemplate>  
                <tr>  
                    <th id="TitleWrap" runat="server" scope="row" class="sf_docTitle">  
                        <asp:HyperLink ID="DownloadLink" runat="server">  
                            <asp:Literal ID="Title" runat="server"></asp:Literal>  
                        </asp:HyperLink>  
                        <asp:Literal ID="Description" runat="server" />  
                    </th>  
                    <td id="DateCreatedWrap" runat="server" class="sf_date">  
                        <sfWeb:DateTimeLiteral ID="DateCreated" runat="server" DateFormatString="<%$Resources:DateFormatString %>"></sfWeb:DateTimeLiteral>  
                    </td> 

    5. Uploaded some documents to a document library and set valid dates for the value of Date Created. Due to a minor bug in the Content View base control, it will not work if the date is not valid.

    6. At the Pages tab, dragged a Download List control and at the Advanced properties tab, in section Filters, set the SortExpresion to DateCreated ASC or DateCreated DESC.

    7. At the third step of the slider wizard, made sure the Upload date checkbox is cleared; otherwise it will not work.  

    The modified templates are in the attached archive.

    All the best,

    Rebecca
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Erik
    Erik avatar
    34 posts
    Registered:
    06 Dec 2007
    27 May 2008
    Link to this post
    The code you gave does add the fields to the form but when I fill in a date for a document, the new date is not being saved.  Everytime it just reverts back to "1/1/1980".

    Also, I would like to enable the date field in the Batch Edit view so I don't have to keep clicking on each document to fill in the date.  I have modified the BatchEditor.ascx file to add the date picker, which worked fine, but the same behavior is happening there where it won't save the date I input into the field.  All date fields default to 1/1/1980 and nothing I do will overwrite this value.

    Help on this please,

    Erik
  5. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    29 May 2008
    Link to this post
    Hi Erik,

    I have tested your scenario with what Rebecca showed and everything seems to work just fine. The only case when the Date is not saved is when the ID of the control you use (RadDatePicker in this case) is different than the key of the meta field you've added. The control we use to save the metadata for any edited content item, searches for something in the template with an ID exactly as the key of the metafield, and in case it doesn't find it, so no metadata is saved. Please make sure that the values are the same in all places, as DateCreated and Libraries.DateCreated are in Rebecca's example and let us know if you still have problems.

    Greetings,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Ritu
    Ritu avatar
    4 posts
    Registered:
    25 Jul 2007
    25 Jun 2008
    Link to this post
    Hello,

    I needed to do similar changes to a already existing Library in my site. I have done everything as mentioned. The only change i did was -
    in web.config the entry i added had a default value of now(). :
    <add key="Libraries.DateCreated" valueType="DateTime" visible="True" searchable="True" sortable="True" defaultValue="#Now" />

    Now i have 3 problems -
    1) When i upload a new document / or edit existing I get the value in date picker as "01/01/1980". I want it to be "system date".Though the date does get saved, if i change it.
    2) I already have about a 100 documents in different libraries in the site. How can i get them all to have the value they were actually uploaded on (The "Uploaded on" field we see in the admin side)
    3) When in advanced settings of Dropdown List Control, i set the SortExpression as "DateCreated DESC" and Save it, Only the new document i added after above change gets displayed.  Why dont the others documents come in? I did manually add date for each document in this particular library (the one on which i was trying this).

    Can you help me in these?

    Thanks,
    Ritika
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    26 Jun 2008
    Link to this post
    Hello Ritu,

    1. When i upload a new document / or edit existing I get the value in date picker as "01/01/1980". I want it to be "system date".Though the date does get saved, if i change it.
    That's true. We replicated the issue and will investigate it as soon as possible. The temporary workaround what you suggested - to select a date for the items by yourself and save it.

    2. I already have about a 100 documents in different libraries in the site. How can i get them all to have the value they were actually uploaded on (The "Uploaded on" field we see in the admin side)
    There are two approaches here, manually to change each document and to write some code in global.asax file in order to do it automatically for your old items. Here's our suggestion:
    1. In the global.asax file (located in your project's root directory), create an event handler for OnExecute() event. This event is being called when you attempt to save an item.
    2. One of the arguments of OnExecute is IContentItem - this the current item that is saved. You should check whether it has DateCreated meta data set, and set if needed.
    3. Run the Batch Edit option. This will force OnExecute() event to be thrown for every single item in the library, so Step 2 will be done for every item.
    3.When in advanced settings of Dropdown List Control, i set the SortExpression as "DateCreated DESC" and Save it, Only the new document i added after above change gets displayed.  Why dont the others documents come in? I did manually add date for each document in this particular library (the one on which i was trying this).
     

    Please follow the KB article How to apply the new metafields to old items in a module.

    Don't hesitate to contact us again if you have any other questions.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. Erik
    Erik avatar
    34 posts
    Registered:
    06 Dec 2007
    27 Jun 2008
    Link to this post
    When i do step 4 I refresh the page with a DownloadList control on it (with Table mode set as the view) and i get an Object Reference not set to an instance of an object.

    Is there somewhere else I have to declare something?
  9. Ritu
    Ritu avatar
    4 posts
    Registered:
    25 Jul 2007
    29 Jun 2008
    Link to this post

    Hello Georgi,

    Thanks for your help.

    Point number 3 worked fine using the KB article. As for point 2, Can I have a detailed explanation of how to do that, or an example? That would help me implement the suggestion you have given.

    Thanks and Regards,
    Ritika

  10. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    30 Jun 2008
    Link to this post
    Hi,

    Erik,
    You shouldn't declare it anywhere else. Could you try resetting the application and see whether you get the same error? Also, could you try to see if you replicate the issue with the attached to Rebecca's post templates?

    Rita,
    Highlighted is the code which you should add in global.asax file:

    void Application_Start(object sender, EventArgs e)  
        // Code that runs on application startup 
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing); 
     
    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        if (e.CommandName != "DeleteContent"
        { 
            Telerik.Cms.Engine.IContent cnt = e.CommandArguments as Telerik.Cms.Engine.IContent; 
            if (cnt != null
            { 
                object o = cnt.GetMetaData("DateCreated"); 
                if (o == null || (DateTime)o != DateTime.MinValue) 
                    cnt.SetMetaData("DateCreated", cnt.DateCreated); 
            } 
        } 

    When you "Batch Edit" your library items, this code will be executed for each of them. It's checking whether there's DateCreated metafield set, and sets such (the upload date) if needed.

    I hope the provided information is helpful.

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Erik
    Erik avatar
    34 posts
    Registered:
    06 Dec 2007
    02 Jul 2008
    Link to this post
    Okay, after some time fiddling with it, I think I finally get it.  I now am able to upload documents and then provide a "Published Date" attribute that I can display in the table mode view and sort on, so initial Mission Accomplished!

    My next dilemma is how to control the sorting of the documents in table mode after clicking on a tag name.  Right now I sort my documents by PublishedDate in Descending order.  When I click on a tag, that sorting goes away and puts the documents in a somewhat random order, (i'm assuming it's sorting it by last modified date).  I want that sorting to persist upon clicking on a tag.  Can you tell me how to accomplish this?

    Thanks for your help,
    Erik
  12. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    07 Jul 2008
    Link to this post
    Hi Erik,

    The Download List control does not support sorting on a column click out of the box. To achieve this you need to create your own control that extends the base one. Please, find instructions below:

        1. Create a copy of the original template (~/Sitefinity/ControlTemplates/Libraries/DownloadListTableModeTemplate.ascx) and modify the copy. You should replace the Literal controls in the repeater HeaderTemplate with LinkButton controls:
       
    <asp:Repeater ID="repeater" runat="server"
        <HeaderTemplate> 
            <table class="sf_libraryGrid"
                <thead> 
                <tr> 
                    <th scope="col" id="TitleWrap" runat="server"><asp:LinkButton ID="btnName" runat="server" Text="<%$Resources:Title %>" CommandName="Name"></asp:LinkButton></th

        2. Create a class that derives from the DownloadList base class
    • Set TableModeTemplatePath property to point to the new template in the CreateChildControls method
    • Implement a custom sorting in the repeater:
    protected override void SetItemHeaderContent(Control itemContainer, IContent contentItem) 
            { 
                base.SetItemHeaderContent(itemContainer, contentItem); 
                LinkButton btn = itemContainer.FindControl("btnName"as LinkButton; 
                if (btn != null
                    btn.Command += new CommandEventHandler(btn_Command); 
            } 
     
            void btn_Command(object sender, CommandEventArgs e) 
            { 
                this.SortData(e.CommandName); 
            } 
            
            void SortData(string sortExpression) 
            { 
                if (ViewState["SortOrder"] == null
                { 
                    ViewState["SortOrder"] = " ASC"
                } 
                else if (ViewState["SortOrder"].ToString() == " ASC"
                { 
                    ViewState["SortOrder"] = " DESC"
                } 
                else 
                { 
                    ViewState["SortOrder"] = " ASC"
                } 
     
                this.SortExpression = sortExpression + ViewState["SortOrder"]; 
     
                this.ListContainer.RepeaterControl.DataBind(); 
            } 

    I hope that helps.
    Please, let us know if you need any more assistance.

    Kind regards,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  13. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    11 Aug 2008
    Link to this post
    Hello Sitefinity,

    I followed the exact instructions on adding the DateCreated column to Sitefinity libraries. I filled in a date with all my items and then the items are showing up, with the new column.

    But when I set the SortExpression filter to this new DateCreated field, the list with downloadable items is empty. I see the paging going from 1 to 4, but I don't see any items.

    Does it have to be an english date format?

    What could be the problem?

    Daniel
  14. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    12 Aug 2008
    Link to this post
    Hi Daniel,

    We have a problem with the meta fields. Basically the sorting expressions with the new meta field would work only with libraries created after adding the mentioned metafield. Fortunately, this issue could be fixed by executing the following script against your database:

    update  
        sf_GCMetaData 
    set CntPrntID =  
        ( 
        select distinct 
            CntPrntID  
        from  
            sf_GCMetaData as meta  
        where  
            meta.ContentID = sf_GCMetaData.ContentID 
            and meta.LangID = sf_GCMetaData.LangID 
            and meta.Application = sf_GCMetaData.Application 
            and meta.ContentID = sf_GCMetaData.ContentID 
            and meta.CntPrntID is not null 
        ) 
    where  
        CntPrntID is null 

    Please make sure you back up your database just in case. You should be now able to sort the items in all the libraries.

    Let us know if you have any other questions.

    All the best,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  15. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    12 Aug 2008
    Link to this post

    Hi Georgi,

    That solved my problem. Thanks!

    Daniel

  16. kevin
    kevin avatar
    1 posts
    Registered:
    09 Apr 2009
    05 May 2009
    Link to this post
    I've created a custom class that inherits from the DownloadList class.  Yet, when I dragged my custom class onto a page, the control still shows as Download List and would not fire run the constructor method.  Any idea?
  17. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    06 May 2009
    Link to this post
    Hello kevin,

    I created a bare bone sample that works at my end. I register the custom control in my web.config file . Generally the constructor of the class you are inheriting from is called.

    Sample

    // DownloadList constructor is called 
    public class MyDownloadList : DownloadList  
        public MyDownloadList() 
        { 
        } 
     
        protected override void CreateChildControls() 
        { 
            base.CreateChildControls(); 
            this.TableModeTemplatePath = ""
        } 
     
        protected override void SetItemHeaderContent(System.Web.UI.Control itemContainer, Telerik.Cms.Engine.IContent contentItem) 
        { 
            base.SetItemHeaderContent(itemContainer, contentItem); 
        }   

    I hope this helps.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  18. Garry Clark
    Garry Clark avatar
    26 posts
    Registered:
    27 Jan 2005
    06 Oct 2009
    Link to this post
    Hey guys,
    I am needing this type of functionality as well and I have downloaded the templates for the ControlPanelEdit.ascx and the DownloadListTableModeTemplate.ascx. I also created the MetaFields and modified the document library as shown above. These templates did not exist in the described locations above so I just simply placed the downloaded ones in those folders.

    I get no errors, but I am also not seeing a DateCreated field/control in either the edit nor the download list. What am I missing? Do I not need to tell Sitefinity to use those templates in Telerik.Sitefinity.Configuration.ControlsConfig.xml or in the TemplatePath of the DownloadList control for these to show up or is Sitefinity supposed to just use them if they are there?

    Oh also I'm using 3.7 SP1 so do I need to download the external templates and modify them or would the ones in this post still work?

    Thanks in advance!

    P.S. Reason I am having to do this is for some reason the download list is not following the default sort method. I'm thinking it maybe because I am applying a filter, but that doesn't really make sense to me either so I decided to just try this method to override the sort and sort it the way I need/want.
  19. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    06 Oct 2009
    Link to this post
    Hello Garry Clark,

    The best practice here would be to modify the external templates for your version, rather than using the ones from this thread. You have guessed right that you have to tell the download list control to use those templates too. You can do it by using the ItemListTemplatePath in the control editor, see attached image.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  20. Garry Clark
    Garry Clark avatar
    26 posts
    Registered:
    27 Jan 2005
    06 Oct 2009
    Link to this post
    Radoslav,
    Thanks for such a quick reply. Can you also post the edit for the Telerik.Sitefinity.Configuration.ControlsConfig.xml as well please?

    Thank you again!
  21. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    06 Oct 2009
    Link to this post
    Hi Garry Clark,

    Unfortunately the controls config file will not work with the Download List control, so you should stick with the method from my previous reply.

    I know that this is not very convenient when having more than one download list control, and I must apologize for the inconvenience.

    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  22. Garry Clark
    Garry Clark avatar
    26 posts
    Registered:
    27 Jan 2005
    06 Oct 2009
    Link to this post
    Radoslav,
    Thanks again for such a quick post, but I am not seeing the CreatedDate field in the DocumentManager portion so I have no way of selecting a DateCreated at all that is why I was think a modification to the Telerik.Sitefinity.Configuration.ControlsConfig.xml was needed. If it is supposed to create it automatically once I add the Meta field definitions, then any idea why it would not be doing so?

    Thanks again!
  23. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    06 Oct 2009
    Link to this post
    Hello Garry Clark,

    I am not sure I understand correctly what you are trying to do. Every content item (that includes documents uploaded in libraries)  have DateCreated property, that is not a metafield. It appears in the external template for the Dowload List view.

    However if you have created a custom metafield called CreatedDate and wish to display it in the table view, setting the external template from the control editor is the same as through the controls config line. Since the CreatedDate is a custom metafield you will have to add it to the table view manually, it will not get created automatically. Here is how it can be added to the table view. First add the table header column to the rest of the <th> tags:
    <th scope="col" id="CreatedDateWrap" runat="server"><asp:Literal ID="Literal12" runat="server" Text="Created date"/></th> 
     

    then add the entry to the table rows to:
    <td id="CreatedDateWrap" runat="server"><asp:Literal ID="CreatedDate" runat="server"/></td> 
     

    Note that the highlighted literal must have the same ID as the meta field.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
23 posts, 0 answered