SharePoint Dragons

Nikander & Margriet on SharePoint

A web part editor part containing a drop down list

This post contains the code required for making a SharePoint 2010 web part that uses an editor part that contains a drop down list.

Here’s the code for the web part editor part:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

namespace SharePointProject3.WebPart1
{
    public class WebPart1EditorPart : EditorPart
    {
        // Reference to the web part that uses this editor part, the parent web part needs to implement the IWebEditable interface to support custom editing controls
        protected WebPart1 ParentWebPart { get; set; }
        protected DropDownList EditorChoices { get; set; }

        /// <summary>
        /// Does editor part init settings
        /// </summary>
        public WebPart1EditorPart()
        {
            Title = “Make a choice – good name here”;
        }

        /// <summary>
        /// Creates the dll
        /// </summary>
        protected override void CreateChildControls()
        {
            EditorChoices = new DropDownList();
            EditorChoices.Items.Add(new ListItem(“AAA”, “1”));
            EditorChoices.Items.Add(new ListItem(“BBB”, “2”));
            EditorChoices.Items.Add(new ListItem(“CCC”, “3”));
            Controls.Add(EditorChoices);

            base.CreateChildControls();
            ChildControlsCreated = true;
        }

        /// <summary>
        /// Reads current value from parent web part and show that in the ddl
        /// </summary>
        public override void SyncChanges()
        {
            EnsureChildControls();
            ParentWebPart = WebPartToEdit as WebPart1;

            if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
            {
                ListItem item = EditorChoices.Items.FindByValue(ParentWebPart.MyEditorPartChoice);
                if (item != null) item.Selected = true;
            }
        }

        /// <summary>
        /// Applies change in editor part ddl to the parent web part
        /// </summary>
        /// <returns></returns>
        public override bool ApplyChanges()
        {
            try
            {
                EnsureChildControls();
                ParentWebPart = WebPartToEdit as WebPart1;

                if (ParentWebPart != null && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
                {
                    ParentWebPart.MyEditorPartChoice = EditorChoices.SelectedValue;
                }

                // The operation was succesful
                return true;
            }
            catch
            {
                // Because an error has occurred, the SyncChanges() method won’t be invoked.
                return false;
            }
        }
    }
}

Here’s the code for the web part itself:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace SharePointProject3.WebPart1
{
    [ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart, IWebEditable
    {
        protected override void CreateChildControls()
        {
            var lbl = new Label();

            if (MyEditorPartChoice == null)
                lbl.Text = “Make a choice first”;
            else
                lbl.Text = “The value from the editor part choice: ” + MyEditorPartChoice;
           
            Controls.Add(lbl);
        }

        /// <summary>
        /// Contains the value of the choice in the editor part drop down list
        ///
        /// Set the Personalizable attribute to true,
        /// to allow for personalization of tabs by users.
        /// This causes  this property to be shown in the web part tool pane and lets SharePoint take care of the storage/retrieval of this property in the SharePoint content database.
        /// </summary>
        [Personalizable(true)]
        public string MyEditorPartChoice { get; set; }

        /// <summary>
        /// Creates custom editor parts here and assigns a unique id to each part
        /// </summary>
        /// <returns>All custom editor parts used by this web part</returns>
        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            var editors = new List<EditorPart>();           
            var editorPart = new WebPart1EditorPart();
            editorPart.ID = ID + “_editorPart”;
            editors.Add(editorPart);

            return new EditorPartCollection(editors);
        }

        /// <summary>
        /// Returns parent web part to editor part
        /// </summary>
        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }
    }
}

5 responses to “A web part editor part containing a drop down list

  1. Pingback: An editor part for a visual web part « SharePoint Dragons

  2. roger May 17, 2012 at 6:22 pm

    Would you have the code that you can upload ? Thank you.

  3. sp2010rambler May 18, 2012 at 6:34 pm

    Being a newbie wanted to see the full code solution. Thanks.

  4. roger May 18, 2012 at 6:52 pm

    I am getting the following error when I try to add the WEB PART to the site:
    “Unhandled exception was thrown by the sandboxed code wrapper’s Execute method in the partial trust app domian: @Resources:core,ImportErrorMessage”

    Any idea’s why ? Thanks for your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: