Sunday, June 22, 2014

Programming custom editor part SharePoint 2013

Programming custom editor part SharePoint 2013


This example shows how to create a custom editor part.
In the editor part, a ListBox control is binded to the list of the Lists in the current web and on selecting multiple lists, all the items from all the lists aggregated are shown in the webpart as depicted below.



Below is the code for custom webpart class with properties having the [WebBrowsable(false)]
-------------------------------------------------------------------------------------------------
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 DemoEditorParts.DisplayMultipleItems
{
    [ToolboxItemAttribute(false)]
    public class DisplayMultipleItems : WebPart
    {

        private List<string> listIDs;

        [WebBrowsable(false)]
        [WebDescription("Multiple Lists")]
        [Personalizable(PersonalizationScope.Shared)]
        public List<string> ListIDs
        {
            get
            {
                return listIDs;
            }
            set
            {

                listIDs = value;
            }
        }

        Dictionary<string, List<string>> allItemTitles = new Dictionary<string, List<string>>();

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {

                if (null == ListIDs)
                {
                    return;
                }
                SPWeb currentWeb = SPContext.Current.Web;

                foreach (var listId in ListIDs)
                {
                    SPList list = currentWeb.Lists[new Guid(listId)];
                    if (null == list)
                    {
                        continue;
                    }
                    if (!allItemTitles.ContainsKey(list.Title))
                    {
                        allItemTitles.Add(list.Title, new List<string>());
                    }
                    SPListItemCollection allItems = list.Items;
                    foreach (SPListItem item in allItems)
                    {
                        allItemTitles[list.Title].Add(item.Title);
                    }
                }


            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl(ex.Message));
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            foreach (var list in allItemTitles)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                writer.Write(list.Key);
                foreach (var title in list.Value)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    writer.Write(title);
                    writer.RenderEndTag();
                }
                writer.RenderEndTag();
            }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();
            DisplayListsEditorPart editor = new DisplayListsEditorPart();
            editor.ID = this.ID + "_DisplayListsEditorPart";
            editor.Title = "Display Lists EditorPart";
            editors.Add(editor);

            return new EditorPartCollection(base.CreateEditorParts(), editors);
        }

    }
}
----------------------------------------------------------------------------------------
The below is the code for custom editor part
---------------------------------------------------------------------------------------

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace DemoEditorParts
{
    public class DisplayListsEditorPart : EditorPart
    {
        private ListBox drpLists;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            drpLists = new ListBox();
            drpLists.SelectionMode = ListSelectionMode.Multiple;
            drpLists.ToolTip = "All Lists";

            SPWeb currentWeb = SPContext.Current.Web;
            SPListCollection allLists = currentWeb.Lists;

            foreach (SPList list in allLists)
            {
                drpLists.Items.Add(new ListItem()
                {
                    Text = list.Title,
                    Value = list.ID.ToString()
                });
            }
            this.Controls.Add(drpLists);
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            DisplayMultipleItems.DisplayMultipleItems webpart
                = this.WebPartToEdit as DisplayMultipleItems.DisplayMultipleItems;

            ListItemCollection drpItems = drpLists.Items;
            List<string> selectedItems = new List<string>();
            foreach (ListItem item in drpItems)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Value);
                }
            }
            webpart.ListIDs = selectedItems;
            return true;
        }
        public override void SyncChanges()
        {
            EnsureChildControls();
            DisplayMultipleItems.DisplayMultipleItems webpart
               = this.WebPartToEdit as DisplayMultipleItems.DisplayMultipleItems;
            List<string> lists = webpart.ListIDs;
            if (null == drpLists)
            {
                return;
            }
            ListItemCollection drpItems = drpLists.Items;
            if (null == lists)
            {
                return;
            }
            foreach (var list in lists)
            {
                ListItem drpItem =
                    drpItems.FindByValue(list);
                if (null == drpItem)
                {
                    continue;
                }
                drpItem.Selected = true;
            }
        }
    }
}

---------------------------------------------------------------------------------------------------




No comments:

Post a Comment