Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I found this post which claims to show you how to do it:

http://blog.concurrency.com/sharepoint/create-a-custom-web-part-for-sharepoint-2010/comment-page-1/#comment-1851

But after carefully following the instructions and downloading the code, I have not been able to get it to work as expected.

I posted a similar question here, but the answers only told me how I could load a drop down list with static values. I would like to load it from a SharePoint list.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Abe you said:

I downloaded the source but it only appears to work with the static drop down list of items. I followed your instructions but was never able to get the drop down working with an internal list. Is this possible to do by following the instructions you have provided? If so, would it be possible to get you to post up the working code for that?

In the source you need to add an override of CreateEditorParts

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.iwebeditable.createeditorparts.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpart.createeditorparts.aspx

public override EditorPartCollection CreateEditorParts() {
    ArrayList editorArray = new ArrayList();
    MyEditorPart edPart = new MyEditorPart();
    edPart.ID = this.ID + "_editorPart1";
    editorArray.Add(edPart);
    EditorPartCollection editorParts = new EditorPartCollection(editorArray);
    return editorParts;
}

So the webpart will use the custom editor parts.

share|improve this answer
    
You rock, I will give this a shot. –  Abe Miessler Apr 15 '11 at 15:01
    
Worked like a charm, thanks! –  Abe Miessler Apr 18 '11 at 4:01

I have a custom ToolPart that reads from a database, but you could change it to read from a SPList instead

public class CustomToolPart : ToolPart {

    protected override void CreateChildControls() {
        ddlCustom = new DropDownList();
        ddlCustom.ID = "ddlCustom";
        ddlCustom.ToolTip = "Custom";
        try {
            using (SqlConnection conn = new SqlConnection(this.webPart.ConnectionString)) {
                conn.Open();
                SqlCommand cmd = new SqlCommand(@"select id,name from [sometableftdb_forums]", conn);
                SqlDataAdapter rs = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                rs.Fill(ds, "Data");
                ddlCustom.DataSource = ds;
                ddlCustom.DataTextField = "name";
                ddlCustom.DataValueField = "id";
                ddlCustom.DataBind();
                ddlCustom.Items.Insert(0, new ListItem("(choose)", "0"));
            }

            //instead add code here for SPSite/SPWeb/SPList

        } catch (SqlException ex) {
            ddlCustom.Items.Insert(0, new ListItem("(sql error)", "0"));
        }
        ListItem item = ddlCustom.Items.FindByValue((webPart != null ? webPart.CustomValueId : 0).ToString());
        if (item != null) {
            ddlCustom.SelectedIndex = -1;
            item.Selected = true;
        }
        Controls.Add(ddlCustom);
    }
}
share|improve this answer
    
I believe this approach is for web parts created as SharePoint WSS 2.0 web parts (Microsoft.SharePoint.WebPartPages.WebPart) rather than ASP.Net web parts as the questioner is referring to. (Note that there are circumstances when one may need the former). –  Chloraphil Feb 12 '13 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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