0

I want to create a custom webpart property which will be a dropdown list. and i want to load values from a sharepoint list. So far i could only find a way to provide static values for list contents. Is there some way i can load the values from a sharepoint list?Any sort of help will be appreaciated

Thank you

   public enum channelList { Health, Wealth, Life };

   public channelList ChannelTypes;

   [WebBrowsable(true),
   WebDisplayName("Choose Channel Type"),
   WebDescription(""),
   Personalizable(PersonalizationScope.Shared),
   Category("Extended Settings"),
   DefaultValue("Select a Channel Type")]
   public channelList _ChannelTypes
   {
        get { return ChannelTypes; }
        set { ChannelTypes = value; }
   }

i want to provide values to enum ChannelList from a sharepoint list

2 Answers 2

3

Mallu,

This can be achieved by creating Editorpart for your custom web part.

  1. Lets say the web part that we are building is RightNav.ascx
  2. Create a .CS file under your web part (.ascx)

enter image description here

  1. Full code of the EditorPart.cs is as follows

You may have to edit the code as per your requirements

class RightNavEditorPart : EditorPart
        {
            #region Properties
            public string ListId { get; set; }
            public string ListName { get; set; }
            RightNav ParentWebPart { get; set; }
            #endregion

            #region Controls
            public Label ListPrompt = new Label() { Text = "Right Nav Links" };
            public DropDownList ddlLists = new DropDownList() { Width = 175, ID = "ddlLists" };
            #endregion

            #region Apply and Sync Changes
            public override bool ApplyChanges()
            {
                bool returnValue = false;
                try
                {
                    EnsureChildControls();
                    ParentWebPart = WebPartToEdit as RightNav;
                    if (ParentWebPart != null)
                    {
                        ParentWebPart.ListId = GetListSelectedValue(ddlLists);
                        ParentWebPart.ListName = GetListSelectedText(ddlLists);
                        returnValue = true;
                    }
                }
                catch (Exception ex)
                {
                    //Logging.Log(ex);
                }
                return returnValue;
            }
            public override void SyncChanges()
            {
                EnsureChildControls();
                ParentWebPart = WebPartToEdit as RightNav;
                if (ParentWebPart != null)
                {
                    SetListSelectedText(ParentWebPart.ListName, "--Select List--", ddlLists);
                }
            }
            #endregion

            #region Methods
            private string GetListSelectedValue(DropDownList ddl)
            {
                string returnValue = string.Empty;
                if (ddl.SelectedIndex != 0)
                {
                    returnValue = ddl.SelectedValue;
                }
                return returnValue;
            }
            public string GetListSelectedText(DropDownList ddl)
            {
                string returnValue = string.Empty;
                if (ddl.SelectedIndex != 0)
                {
                    returnValue = ddl.SelectedItem.Text;
                }
                return returnValue;
            }
            private void SetListSelectedText(string text, string defaultValue, DropDownList ddl)
            {
                if (!string.IsNullOrEmpty(text))
                {
                    if (ddl.Items.FindByText(text) != null)
                    {
                        ddl.Items.FindByText(text).Selected = true;

                    }
                }
                else if (!string.IsNullOrEmpty(defaultValue))
                {
                    if (ddl.Items.FindByText(defaultValue) != null)
                    {
                        for (int i = 0; i < ddl.Items.Count; i++)
                        {
                            if (ddl.Items[i].Text == defaultValue)
                            {
                                ddl.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Control Overrides
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                this.ChromeType = PartChromeType.None;
                try
                {
                    Controls.Add(ListPrompt);
                    Controls.Add(ddlLists);
                    LoadWebLists();
                }
                catch (Exception ex)
                {
                    // Log(ex);
                }
            }
            protected override void RenderContents(HtmlTextWriter writer)
            {
                try
                {
                    writer.Write("<div class='ms-TPBody' id='SGRightNav-editorpart'>");
                    writer.Write("<table>");
                    writer.Write("<tr><td>");
                    writer.Write("<div class='UserSectionHead'>");
                    ListPrompt.RenderControl(writer);
                    writer.Write("</div>");
                    writer.Write("<div class='UserSectionBody'>");
                    writer.Write("<div class='UserControlGroup'>");
                    ddlLists.RenderControl(writer);
                    writer.Write("</div>");
                    writer.Write("</div>");
                    writer.Write("</td></tr>");
                    writer.Write("</table>");
                    writer.Write("</div>");
                }
                catch (Exception ex)
                {
                    //Logging.Log(ex);
                }
            }
            #endregion

            #region Data Loading Methods
            private void LoadWebLists()
            {
                try
                {
                    //SPSecurity.RunWithElevatedPrivileges(delegate()
                    //{
                    ddlLists.Items.Clear();
                    List<ListItem> imageListItems = new List<ListItem>();
                    SPListCollection docLibraryColl = SPContext.Current.Web.GetListsOfType(SPBaseType.GenericList);

                    foreach (SPList list in docLibraryColl)
                    {
                        if (!list.Hidden)
                        {
                            //if (list.GetType() == typeof(SPPictureLibrary) ||
                            //    list.TemplateFeatureId.ToString() == "4bcccd62-dcaf-46dc-a7d4-e38277ef33f4" ||
                            //    list.TemplateFeatureId.ToString() == "bb8630c8-73c5-4a62-abf8-6d7d8bcb0e81")
                            {
                                imageListItems.Add(new ListItem() { Text = list.Title, Value = list.ID.ToString() });
                            }
                        }
                    }
                    imageListItems.Sort(delegate(ListItem l1, ListItem l2)
                    {
                        return l1.Text.CompareTo(l2.Text);
                    });
                    imageListItems.Insert(0, new ListItem() { Text = "--Select List--" });
                    foreach (ListItem li in imageListItems)
                    {
                        ddlLists.Items.Add(li);
                    }
                    ParentWebPart = WebPartToEdit as RightNav;
                    SetListSelectedText(ParentWebPart.ListName, "--Select List--", ddlLists);
                    //});
                }
                catch (Exception ex)
                {
                    // Log(ex);
                }
            }
            #endregion
        }
  1. In your .ascx.cs file place the following method

You may have to write some extra code to utilize the properties here in the .ascx.cs file

public override EditorPartCollection CreateEditorParts()
        {
            EditorPartCollection epc = null;
            try
            {
                List<EditorPart> lstEditorPart = new List<EditorPart>();
                RightNavEditorPart ep = new RightNavEditorPart();
                ep.ID = this.ID + "_EditorPart";
                lstEditorPart.Add(ep);
                epc = base.CreateEditorParts();
                epc = new EditorPartCollection(epc, lstEditorPart);
            }
            catch (Exception ex)
            {

            }
            return epc;
        }

Let me know if you have any questions

0

You're basically asking how one can dynamically add values to an enum. The short answer is it is difficult/not worth the hassle. Others, it seems, have had success with a workaround where you create a custom tool pane and override the rendering method to draw your own drop down.

See here.

Incidentally, a similar question has been asked previously, here.

2
  • I think you didnt understand my question. what i want to do is create a custom webpart property , and not a dropdown in the usercontrol Commented May 28, 2015 at 10:40
  • 1
    You've since added code to your question, which has made it clearer. What you're effectively asking is how one can dynamically add values to an enum. Check this: social.msdn.microsoft.com/Forums/en-US/…
    – Submits
    Commented May 28, 2015 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.