Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top