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

Forums / General Discussions / Adding custom field to Library

Adding custom field to Library

27 posts, 0 answered
  1. Gökhan Yalçın
    Gökhan Yalçın avatar
    21 posts
    Registered:
    26 Nov 2009
    16 Dec 2009
    Link to this post
    Hi everyone ,

    I want to add a description field to Image Library . ( Not to an image in the library ) . Is there any easy way to do that ?

    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Dec 2009
    Link to this post
    Hi Gökhan Yalçın,

    There is a way that you can do this, but it is not so convenient. Below are the steps that you have to follow.

    1. Map the external template for Telerik.Libraries.WebControls.Admin.LibraryNew and Telerik.Libraries.WebControls.Admin.LibraryEdit. You can take a look at the following KB article How to map an template

    2. Add TextBox control to each of the Views that will be used to add description data for the library.

    3. Create a new database table that will contain two columns - one should be the Library ID and the second should be the textbox value.

    4. You need to subscribe "Create this library" to persists the data in the custom table.

    5. You need to retrieve the data in Telerik.Libraries.WebControls.Admin.LibraryEdit when you edit a library.

    I hope that this helps.


    Best wishes,
    Ivan Dimitrov
    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.
  3. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    16 Dec 2009
    Link to this post
    Hi Ivan,

    Funny thing, I just started to work on this last night as I need this functionality too. If you have it handy could you supply sample for steps 4 & 5? That would be a huge help for me! :)

    Cheers,
    Phill
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Dec 2009
    Link to this post
    Hi Phill Hodgkinson,

    Actually there should be a small change here. You have to replace the views of LibraryEdit and LibraryNew. Then you have to override InitializeControls and subscribe for OnSaved event. From the CommandArgument you can get the Library ID and persist the textbox to the database. You can create a simple dbml class and use LINQ to SQL using a DataContext object

    If you get stuck, write back to me.

    All the best,
    Ivan Dimitrov
    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.
  5. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    16 Dec 2009
    Link to this post
    Hi Ivan,

    As per my previous post I'm ok with replacing the templates and doing the linq to sql it's step 4 & 5 that I need help with. Or as per your update, the "override InitializeControls and subscribe for OnSaved". Is "initializeControls" where you would retrieve the data and populate the text box for editing? It might be a terminology thing, but I've never "subscribed for OnSaved" so I don't even know where to start here. I don't need anything complete, just a starter sample. :)

    Thanks again!
    Cheers,
    Phill
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Dec 2009
    Link to this post
    Hi Phill Hodgkinson,

     Below is the implementation of the both views

    LibraryEdit

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Libraries.WebControls.Admin;
    using System.Web.UI.WebControls;
    using Telerik.Libraries;
     
    /// <summary>
    /// Summary description for Class9
    /// </summary>
    public class Class9 : LibraryEdit<LibrariesView>
    {
        public Class9()
        {
     
        }
     
        public override Type LocalizationAssemblyInfo
        {
            get
            {
                return typeof(LibraryEdit<LibrariesView>);
            }
            set
            {
                base.LocalizationAssemblyInfo = value;
            }
        }
     
        public override System.Type AssemblyInfo
        {
            get
            {
                return typeof(LibraryEdit<LibrariesView>);
            }
            set
            {
                base.AssemblyInfo = value;
            }
        }
     
        protected override void InitializeControls(System.Web.UI.Control viewContainer)
        {
            base.InitializeControls(viewContainer);
            Guid id = this.LibraryId;
             
             
        }
     
    }

    LibraryNew

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Libraries.WebControls.Admin;
    using System.Web.UI.WebControls;
    using Telerik.Libraries;
     
    /// <summary>
    /// Summary description for Class10
    /// </summary>
    public class Class10 : LibraryNew<LibrariesView>
    {
        public Class10()
        {
     
        }
     
        public override Type LocalizationAssemblyInfo
        {
            get
            {
                return typeof(LibraryNew<LibrariesView>);
            }
            set
            {
                base.LocalizationAssemblyInfo = value;
            }
        }
     
        public override System.Type AssemblyInfo
        {
            get
            {
                return typeof(LibraryNew<LibrariesView>);
            }
            set
            {
                base.AssemblyInfo = value;
            }
        }
     
        protected override void InitializeControls(System.Web.UI.Control viewContainer)
        {
            base.InitializeControls(viewContainer);
            this.Saved += new CommandEventHandler(Class10_Saved);
        }
     
        void Class10_Saved(object sender, CommandEventArgs e)
        {
            Guid id = new Guid(e.CommandArgument.ToString());
             
        }
     
    }

    Sincerely yours,
    Ivan Dimitrov
    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.
  7. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    17 Dec 2009
    Link to this post
    Hi Ivan,

    Hopefully one last quick question. Thanks again for your help too and Gokhan my apologies for hijacking your thread but I hope this is helping you too. So I've only ever added meta data etc which is quite easy to wire up and add to the admin. In this case I understand what your sample code is doing but I don't know the syntax (and maybe not where) to wire it up. Would I replace the hostType in the ConfigureControlsConfig.xlm file for the LibraryNew and LibraryEdit view settings? (i.e. <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryNew`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/CustomControls/Library/LibraryNew.ascx" />)  and if so, assuming the sample code you provided lives in App_Code, what would the syntax be? I've tried a nubmer of variations and all error. Or am I totally off and there's another way this should be wired up.

    Thanks again for your help and patience!
    Cheers,
    PHill
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 Dec 2009
    Link to this post
    Hi Phill Hodgkinson,

    Below is the correct syntax

    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries">
      <views>
        <remove name="LibraryEdit`1" />
        <add name="LibraryEdit`1" viewType="Class9, App_Code"></add>
        <remove name="LibraryNew`1" />
        <add name="LibraryNew`1" viewType="Class10, App_Code"></add>
      </views>
    </viewSettings>

    More information at Replacing a child view with another one


    Kind regards,
    Ivan Dimitrov
    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.
  9. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    17 Dec 2009
    Link to this post
    Hi Ivan,

    Thanks again. Is there somewhere in the User manual that details this process as I'm unable to find it. The sample you sent me makes sense but with that I no longer have my custom user controls that have the description text box. If I add the custom view classes in the following way, I get an error: 

    Unable to cast object of type 'Telerik.Libraries.WebControls.Admin.LibraryEdit`1[Telerik.Libraries.WebControls.Admin.LibrariesView]' to type 'Telerik.Libraries.WebControls.Admin.LibrariesView'.


        <!--Creates a new library.--> 
        <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryNew`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/CustomControls/Library/LibraryNew.ascx"
          <views> 
            <remove name="LibraryNew`1" /> 
            <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code"></add> 
          </views> 
        </viewSettings> 
        <!--Edits the settings of a library.--> 
        <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryEdit`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/CustomControls/Library/LibraryEdit.ascx"
          <views> 
            <remove name="LibraryEdit`1" /> 
            <add name="LibraryEdit`1" viewType="CustomLibraryEdit, App_Code"></add> 
          </views>  
        </viewSettings> 

    I hate to keep bugging you with this but I can't seem to find any documentation on this outside the usual customization of meta data fields on generic content.

    Thanks again for your help!
    Phill
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 Dec 2009
    Link to this post
    Hello Phill Hodgkinson,

    As far as I see the hostType in the sample I sent you is different. So, please give a try to the host type that I am using.


    Kind regards,
    Ivan Dimitrov
    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.
  11. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    17 Dec 2009
    Link to this post
    Using the following (I believe this matches what you provided), I get the error: 

    An item with the same key has already been added.


        <!--Creates a new library.--> 
        <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries" layoutTemplatePath="~/CustomControls/Library/LibraryNew.ascx"
          <views> 
            <remove name="LibraryNew`1" /> 
            <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code"></add> 
          </views> 
        </viewSettings> 
        <!--Edits the settings of a library.--> 
        <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries" layoutTemplatePath="~/CustomControls/Library/LibraryEdit.ascx"
          <views> 
            <remove name="LibraryEdit`1" /> 
            <add name="LibraryEdit`1" viewType="CustomLibraryEdit, App_Code"></add> 
          </views>  
        </viewSettings> 

    This is obviously because the hostType "... LibrariesView" is being set twice. If i remove one it works. Then I realize that it's replacing the entire libraryview and not just the LibraryNew control. So when I click on Images&Documents I don't see the LibraryView but the LibraryNew control. This makes sense as this is what my layoutTemplatePath is telling it to use.

    I think the answer here is that adding a description to a library is waaaay more complicated than it should be. Thanks for your help but I'm going to stop wasting my time and your time and find another approach.

    For future users going down this path, can you point to any documentation or working samples of this?

    Thanks anyway!
    Phill
  12. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    08 Jan 2010
    Link to this post
    Hello.

    I'm thinking this should still work. I am doing somethiing similar. I am adding a textbox ("Comments") and a dropdownlist box ("ApprovalGroup") to the form. Everything shows up correctly, however, the values do not persist back to the database. I implemented the same solution for generic content which does persist back to the database. My only problem with the generic content customization is I don't know how to retrieve the current value to set the "Approval Group" field in edit mode.

    I see the code above that I believe is used to persist the values (which I still haven't implemented yet), but I'm not sure how that helps when retrieving a persisted value. Any ideas on how to proceed with this?

    Thanks.
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Jan 2010
    Link to this post
    Hi Mike Boyle,

    You were able to get similar solution with GenericContent, because most probably you have used MetaKeys. Here we are working with ILibrary objects. To get the data when you edit an Library you have to override LibraryEdit view and its tempates. To insert or retrieve data you can use the methods that your data layer expose - OpenAccess, Nolics, Linq to SQL method etc.

    For content item objects - the library items you can use metakeys.

    Regards,
    Ivan Dimitrov
    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.
  14. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    10 Jan 2010
    Link to this post
    Thanks for your reply. I thought that would be the case.

    In the case of adding custom fields to generic content, we have added a dropdownlist which seems to work. The problem is that I don't know how to retrieve and set the selected value in the dropdownlist to the current value in edit mode. Could you assist me with that?

    Thanks again.
  15. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    11 Jan 2010
    Link to this post
    Per my question above regarding retrieving and setting a dropdownlist value, just let me know if you need more specifics.

    Thanks.
  16. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Jan 2010
    Link to this post
    Hi Mike Boyle,

    How do you persists the DropDownList selectedItem or SelectedValue?

    Greetings,
    Ivan Dimitrov
    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.
  17. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    11 Jan 2010
    Link to this post
    Actually using the same method as I used for a textbox it seems to persist. I may be answering my own question now, but we've done tests where we check the value and send an email based on the value that was selected (ApprovalGroup was the name of the field). Based on the value selected we retrieve all email addresses associated with the group (which, in turn, is associated with a role) and send an email. It's another custom email solution we have implemented.

    Long story short, I may just use the same method we are using to grab the value in order to send the email. Does this make sense to you?

  18. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Jan 2010
    Link to this post
    Hi Mike Boyle,

    Actually using the same method as I used for a textbox it seems to persist.

    I am sorry but I am not sure which method you use. Once again

    "You were able to get similar solution with GenericContent, because most probably you have used MetaKeys. Here we are working with ILibrary objects. To get the data when you edit an Library you have to override LibraryEdit view and its tempates. To insert or retrieve data you can use the methods that your data layer expose - OpenAccess, Nolics, Linq to SQL method etc."

    Best wishes,
    Ivan Dimitrov
    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.
  19. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    11 Jan 2010
    Link to this post
    Sorry - I was referring to the use of MetaKeys as you mentioned earlier. I did not need to implement any data access layer as using metakeys seems to persist the data via the methods provided by sitefinity in the external templates. Let me know if this doesn't make sense to you.
  20. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Jan 2010
    Link to this post
    Hello Mike Boyle,

    It is not possible to persists metadata for ILibrary object. Most probably you want to achieve something different that is not topic of this post discussion which explains how to do this for ILibrary object which does not support metadata.

    All the best,
    Ivan Dimitrov
    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.
  21. Mike Boyle
    Mike Boyle avatar
    22 posts
    Registered:
    17 Dec 2009
    11 Jan 2010
    Link to this post
    I think I'm confusing you.

    My original question was regarding New and Library items. My subsequent question was regarding generic content as in my statement "In the case of adding custom fields to generic content, we have added a dropdownlist which seems to work. "

    The dropdownlist was added to generic content. I realized that the News and Library items would require some data access code. I should probably have created a new discussion thread.

    Sorry for the confusion.
  22. Jiju Alex
    Jiju Alex avatar
    2 posts
    Registered:
    21 Dec 2009
    24 Aug 2010
    Link to this post
    Hi Ivan Dimitrov,

    I was trying your code for a custom field implementation in LibraryNew.ascx. I created  a class as you mentioned and configured in Telerik.Sitefinity.Configuration.ControlsConfig.xml as below

    <!--Creates a new library.-->
    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryNew`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryNew.ascx" />
    <!--Edits the settings of a library.-->
    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryEdit`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryEdit.ascx" />
     
    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries">
      <views >
       
        <remove name="LibraryNew`1" />
        <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code"></add>
      </views>
       
    </viewSettings>

    After I put this new configuration the new fields that I added in LibraryNew.ascx is gone. Event is firing and I am able to get LibraryID thats all fine. But how can I map the layouttemplate back to ~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryNew.ascx" . Please help.
  23. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Aug 2010
    Link to this post
    Hi Jiju Alex,

    I am wondering whether you have created persistence classes to get/save this data?  You can map the view template through ControlsConfig using virtualPath property

    <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code" virtualPath="~/path" description="custom view"></add>


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  24. Jiju Alex
    Jiju Alex avatar
    2 posts
    Registered:
    21 Dec 2009
    24 Aug 2010
    Link to this post
    Hi Ivan Dimitrov,

    Thank you for the quick reply. I tried with virtualPath

    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries">
        <views >
          <remove name="LibraryNew`1" />
          <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code" virtualPath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryNew.ascx"></add>
        </views>
         
      </viewSettings>

    Now Layout is coming properly with the new fields I added, but Library types RadioButtonList is not loading. Please find below the custom class I made.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Libraries.WebControls.Admin;
    using System.Web.UI.WebControls;
    using Telerik.Libraries;
     
    /// <summary>
    /// Summary description for CustomLibraryNew
    /// </summary>
    public class CustomLibraryNew : LibraryNew<LibrariesView>
    {
        public CustomLibraryNew()
        {
     
        }
     
        public override Type LocalizationAssemblyInfo
        {
            get
            {
                return typeof(LibraryNew<LibrariesView>);
            }
            set
            {
                base.LocalizationAssemblyInfo = value;
            }
        }
     
        public override System.Type AssemblyInfo
        {
            get
            {
                return typeof(LibraryNew<LibrariesView>);
            }
            set
            {
                base.AssemblyInfo = value;
            }
        }
     
        protected override void InitializeControls(System.Web.UI.Control viewContainer)
        {
            base.InitializeControls(viewContainer);
            this.Saved += new CommandEventHandler(CustomLibraryNew_Saved);
        }
     
        void CustomLibraryNew_Saved(object sender, CommandEventArgs e)
        {
            Guid id = new Guid(e.CommandArgument.ToString());
            TextBox libraryDescription = (TextBox)this.FindControl("libraryDescription");
     
    "CODE FOR SAVING DATA WILL COME HERE"
        }
     
    }

    Please see attachment also to get an idea about the problem. Please help.
  25. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Aug 2010
    Link to this post
    Hi Jiju Alex,

    The configuration for this case is quite different.

    1. Replace the view
    2. Map the template separately for the new view

    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries"  >
         <views >
           <remove name="LibraryNew`1" />
           <add name="LibraryNew`1" viewType="CustomLibraryNew, App_Code"></add>
         </views>
     
       </viewSettings>
     
       <viewSettings hostType="CustomLibraryNew, App_Code" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryNew1.ascx" />

    Restart the application after the changes above.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  26. Endel
    Endel avatar
    2 posts
    Registered:
    16 Aug 2010
    23 Sep 2010
    Link to this post
    Hey, I found some useful tricks in this thread.

    But I'm a little confused about the dbml file. By default the constructor of my DataContext must receive a string connection or a connection object by parameter. How did I get that in Sitefinity?

    Another doubt: Can I just add a new column to SF_LIBRARIES table and update it? I want to be able to filter my content accourding to this new column.
    (In my case, the new column is a Category, for each Library, not a item in the library)

    Thanks in advance.
  27. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    23 Sep 2010
    Link to this post
    Hi Endel,

    You can add a new columns to existing tables, but it is better to work with separate tables, because your custom column might cause issues during upgrades.

    The DataContext is generated by the Object Relational Designer. Entity classes and this works in the same way in Sitefinity as for each other application. You can use the object designer to set which database and tables will be queried. Additionally here is a simple code, but this is not something specific that comes from Sitefinity

    string connectionString = @"Data Source=server_name;database=database_name;Integrated Security=True;";
     
    using (SqlConnection connection = new SqlConnection(connectionString))
       {
                     connection.Open();
                     DataContext context = new DataContext(connection);
                     // your query goes here
                     connection.Close();
       }


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
27 posts, 0 answered