1

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.

2 Answers 2

3

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.

1
  • You rock, I will give this a shot. Commented Apr 15, 2011 at 15:01
2

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);
    }
}
1
  • 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
    Commented Feb 12, 2013 at 20:48

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.