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

Forums / Developing with Sitefinity / Library Provider Help

Library Provider Help

8 posts, 0 answered
  1. Russell
    Russell avatar
    25 posts
    Registered:
    08 Apr 2009
    03 Mar 2010
    Link to this post
    Hello All,

    I have setup 33 different Library providers so that each department doesn't have access to each others items. This works wonderfully when they go through the Images/Docs module, their provider is set based on roles and then work in the library they need.

    I am now trying to set it up so that when a user is editing a page and needs to add something from the images/docs they are presented with a provider dropdown (with their available providers) that will display the libraries they can choose from.

    Under Ivan's direction I created a custom class that overrides the CreateChildControls() for the ItemSelector.ascx. Currently the dropdown is populating with the correct providers list, but the libraries returned are always from the first provider in my web.config.

    protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                if (this.mode == DisplayMode.LibrariesView)
                {
                    this.CreateSpecificView();
                }
     
                if (this.ProvidersList != null)
                {
                    List<string> LibraryProviders = new List<string>(Telerik.Libraries.Configuration.ConfigHelper.Handler.GenericContentProviders.Count);
                    
                    foreach (Telerik.Libraries.Configuration.GenericContentElement element in Telerik.Libraries.Configuration.ConfigHelper.Handler.GenericContentProviders)
                    {
                        Telerik.Cms.Engine.Security.GlobalPermission perm = new Telerik.Cms.Engine.Security.GlobalPermission(
                           (Telerik.Cms.Engine.Security.GlobalPermissions)LibraryManager.SecurityRoots[element.ProviderName], CrudRights.View);
                       if (perm.CheckDemand())
                           LibraryProviders.Add(element.ProviderName);
                    }
     
                    if (LibraryProviders.Count > 0)
                    {
                        foreach (string name in LibraryProviders)
                        {
                            if (!String.IsNullOrEmpty(name))
                            {
                                ListItem item = new ListItem(name.Replace('_', ' '), name);
                                if (name == this.providerName)
                                    item.Selected = true;
                                ProvidersList.Items.Add(item);
                            }
                        }
                    }
                }
            }
     
     
            protected void CreateSpecificView()
            {
                this.container.ItemsHolder.Visible = false;
                this.container.LibrariesHolder.Visible = true;
     
                ObjectDataSource odsLibs = new ObjectDataSource();
                odsLibs.ID = "LibsDataSource";
                odsLibs.TypeName = "Telerik.Libraries.LibraryManager";
                odsLibs.SelectMethod = "GetAllLibraries";
                odsLibs.SelectCountMethod = "GetLibrariesCount";
                odsLibs.MaximumRowsParameterName = "max";
                odsLibs.StartRowIndexParameterName = "from";
                odsLibs.SortParameterName = "sortExp";
                odsLibs.EnablePaging = true;
                odsLibs.ObjectCreating += new ObjectDataSourceObjectEventHandler(odsLibs_ObjectCreating);
                odsLibs.Selecting += new ObjectDataSourceSelectingEventHandler(odsLibs_Selecting);
                this.container.Controls.Add(odsLibs);
     
                this.container.LibrariesRepeater.ItemCreated += new RepeaterItemEventHandler(LibrariesRepeater_ItemCreated);
                this.container.LibrariesRepeater.DataSource = odsLibs;
                this.container.LibrariesRepeater.DataBind();
            }

    In stepping through the code I can see that the ObjectDataSource always seems to be using that first provider from my web.config and I am stuck on a way to pass the providerName for the ObjectDataSource to use.

    I would greatly appreciate any suggestions!

    Thanks,

    Russell
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Mar 2010
    Link to this post
    Hello Russell,

    Thank you for using our services.

    In the event handler for object creating for your object data source:
    odsLibs.ObjectCreating += new ObjectDataSourceObjectEventHandler(odsLibs_ObjectCreating);

    you should instantiate the the instance of the object data sours with the provider name from the selected value of the providers drop-down:
    void odsLibs_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new Telerik.Libraries.LibraryManager("Value of the selcted index here");
    }

    This should instantiate a library manager with the selected provider name and get libraries from this provider.

    Kind regards,
    Radoslav Georgiev
    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.
  3. Russell
    Russell avatar
    25 posts
    Registered:
    08 Apr 2009
    03 Mar 2010
    Link to this post
    Thanks, Radoslav! That solved passing the providerName, but another issue has been caused in that now the Provider dropdown list is not be populated. After "this.container.LibrariesRepeater.DataBind();" runs the this.ProviderList is being set to null and the dropdown is not being populated.

    Any thoughts?
  4. Mat
    Mat avatar
    23 posts
    Registered:
    05 Nov 2006
    22 Mar 2010
    Link to this post
    Hi Russell,

    I'm trying to do the same but I don't get it to work.
    Can you post the source for the control (ItemSelector.ascx)?

    Greetings,
    Mat
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Mar 2010
    Link to this post
    Hi Mat,

    The code is embedded into Telerik.Libraries.dll. The ItemSelector.ascx is only a template for the Telerik.Libraries.WebControls.ItemSelector class.

    Below is a sample implementation

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Libraries.WebControls;
    using System.Web.UI.WebControls;
    using Telerik.Libraries;
    using Telerik.Cms.Engine;
    using Telerik.Security.Permissions;
    using Telerik.Cms.Engine.Security;
    using System.Web.UI;
    using System.Collections;
     
    /// <summary>
    /// Summary description for CustomSelectorDialog
    /// </summary>
    ///
     
    namespace Sitefinity.Samples
    {
        public class CustomSelectorDialog : ItemSelector
        {
            public CustomSelectorDialog()
            {
            }
     
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                if (this.mode == DisplayMode.LibrariesView)
                {
                    this.CreateSpecificView();
                }
                if (this.mode == DisplayMode.ItemsView)
                {
                    this.CreateSpecificItemsView();
                }
            }
     
            void ProvidersList_SelectedIndexChanged(object sender, EventArgs e)
            {
                int si = ((System.Web.UI.WebControls.ListControl)(sender)).SelectedIndex;
                libraryManager = new LibraryManager(((System.Web.UI.WebControls.ListControl)(sender)).SelectedItem.Text);
                IList items = libraryManager.GetAllLibraries();
     
                this.container.LibrariesRepeater.ItemCreated += new RepeaterItemEventHandler(LibrariesRepeater_ItemCreated);
                this.container.LibrariesRepeater.DataSource = items;
                this.container.LibrariesRepeater.DataBind();
                BindProvidersList();
                if (ProvidersList != null)
                {
                    ProvidersList.SelectedIndex = si;
                }
            }
     
            private void BindProvidersList()
            {
                if (this.ProvidersList != null)
                {
                    ProvidersList.AutoPostBack = true;
                    List<string> LibraryProviders = new List<string>(Telerik.Libraries.Configuration.ConfigHelper.Handler.GenericContentProviders.Count);
                    foreach (Telerik.Libraries.Configuration.GenericContentElement element in Telerik.Libraries.Configuration.ConfigHelper.Handler.GenericContentProviders)
                    {
                        GlobalPermission perm = new GlobalPermission(
                            (GlobalPermissions)LibraryManager.SecurityRoots[element.ProviderName], CrudRights.View);
                        if (perm.CheckDemand())
                            LibraryProviders.Add(element.ProviderName);
                    }
     
                    if (LibraryProviders.Count > 0)
                    {
                        foreach (string name in LibraryProviders)
                        {
                            if (!String.IsNullOrEmpty(name))
                            {
                                ListItem item = new ListItem(name.Replace('_', ' '), name);
                                if (name == this.providerName)
                                    item.Selected = true;
                                ProvidersList.Items.Add(item);
                            }
                        }
     
                    }
     
                }
            }
     
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                if (ProvidersList != null)
                {
                    ProvidersList.SelectedIndexChanged += new EventHandler(ProvidersList_SelectedIndexChanged);
                }
            }
     
            protected void CreateSpecificView()
            {
                this.container.ItemsHolder.Visible = false;
                this.container.LibrariesHolder.Visible = true;
                if (ProvidersList != null)
                {
                    BindProvidersList();
                }
     
     
            }
     
            protected void CreateSpecificItemsView()
            {
                //IMPLEMENT LOGIC FOR SHOWING THE ITEMS.
            }
     
            void LibrariesRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    if (!libraryManager.GetPermission(CrudRights.View).CheckDemand())
                        e.Item.Visible = false;
                    else
                    {
                        Control ctrl = null;
                        ILibrary library = (ILibrary)e.Item.DataItem;
                        int itemsCount = 0;
                        if (this.ShowMode == ViewMode.All)
                        {
                            itemsCount = library.ItemsCount;
                        }
                        else if (this.ShowMode == ViewMode.Images)
                        {
                            itemsCount = libraryManager.ImagesCount(library.ID);
                            if (itemsCount <= 0)
                                 e.Item.Visible = false;
                            else
                                e.Item.Visible = true;
                                
                        }
                        else if (this.ShowMode == ViewMode.Documents)
                        {
                            if (library.TypeName == LibraryInfo.Library_Image_Name)
                            {
                                e.Item.Visible = false;
                            }
                            else
                            {
                                itemsCount = libraryManager.DocumentsCount(library.ID);
                                if (itemsCount <= 0)
                                    e.Item.Visible = false;
                            }
                        }
     
                        ctrl = e.Item.FindControl("imagesCount");
                        if (ctrl != null && ctrl is ITextControl)
                            if (this.ShowMode == ViewMode.Images)
                                (ctrl as ITextControl).Text = itemsCount.ToString() + " " + "images";
                            else
                                (ctrl as ITextControl).Text = itemsCount.ToString() + " " + "items";
     
                        ctrl = e.Item.FindControl("librarySelector");
                        if (ctrl != null && ctrl is IButtonControl)
                        {
                            IButtonControl button = ctrl as IButtonControl;
                            button.CommandArgument = library.ID.ToString();
                            button.Command += new CommandEventHandler(button_Command);
                        }
     
                        if (e.Item.Visible)
                            this.visibleLibraries++;
                    }
                }
     
            }
     
            private void button_Command(object sender, CommandEventArgs e)
            {
                this.selectedLibrary = new Guid((string)e.CommandArgument);
                this.Mode = DisplayMode.ItemsView;
            }
     
            private DisplayMode Mode
            {
                get
                {
                    return this.mode;
                }
                set
                {
                    this.mode = value;
                    this.selectedTabIndex = container.TabStrip.SelectedIndex;
                    this.ChildControlsCreated = false;
                }
            }
     
     
     
            protected virtual DropDownList ProvidersList
            {
                get
                {
                    if (container != null)
                        return container.GetControl<DropDownList>("ProvidersDropDownList", false, Telerik.Framework.Web.TraverseMethod.BreadthFirst);
                    return new DropDownList();
                }
     
            }
     
     
            private string providerName;
            private LibraryManager libraryManager;
     
        }
    }


    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.
  6. Andy
    Andy avatar
    3 posts
    Registered:
    20 Apr 2012
    30 Apr 2012
    Link to this post
    I have followed the directions from the above, but I am having problems getting the customSelector class to run. I have created the class the inherits form the iTemSelector class but the piece of code never runs when I run the application. How is my custom class hooked to the itemSelector.ascx. Please give me some directions on how to get this to work.
  7. Andy Oppong
    Andy Oppong avatar
    1 posts
    Registered:
    28 May 2009
    30 Apr 2012
    Link to this post
    Test
  8. Andy
    Andy avatar
    3 posts
    Registered:
    20 Apr 2012
    01 May 2012
    Link to this post
    Let me rephrase this. For the above scenario, how do I hook up the itemSelector.ascx control template with my new class that inherits from the itemSelector class.? I cannot find any documents on your website about this. Please let me know.
Register for webinar
8 posts, 0 answered