Overview
In the previous post we discussed how we can create configurable webpart properties. We also noticed that the web part properties were ENUM and we could only enter static values as parameter like typing a list name or column name etc.
What if you type the list name incorrectly? Therefore, you would want to choose a list from a asp.net dropdown containing all the available lists and display the items for the selected list in a Gridview. This is were EditorParts come in handy! So lets get started...
Editor Parts
To customize the Web Parts’ configuration user interface you can create custom classes called Editor Parts which are controls hosted in a specific WebPartZone called EditorZone.
ASP.NET 4.0 contains an abstract class called EditorPart, this class is used with the ASP.NET 4.0 WebPart class to create customized tool panes. By inheriting from this control you can customize the look and functionality of the tool pane and use standard ASP.NET constructs such as auto post backs, validations etc.
IWebEditable Interface
To provide a Web Part with a custom Editor Part, you need to override the implementation of the IWebEditable interface, which is implemented by the Web Part base class.
Steps to create an Editor Part
Following are the steps:-
1) Create a Visual web part as mentioned in the the post "Creating a Visual Webpart".
2) Go to the webpart file "MyVisualWP.ascx.cs" and implement the "IWebEditable" interface. Also, add a property named "ListName" as shown in the figure below.
3) Now add a new class named "WPEditor.cs" which inherits from the "EditorPart" class. Create a new Dropdownlist and populate the list with all the Lists in the site. Then add the dropdownlist to the controls collection as shown in the figure below.
4) EditorPart class has 2 important methods:
i) Apply Changes() : This method is used to save any changes to the currently-edited Web Part.
ii) SyncChanges() : This method is used to load the current configuration from Web Part Properties.
5) Following is the complete code "WPEditor.cs" class file.
6) Make the following changes in the user controls code behind file "MyVisualWPUserControl.ascx.cs" which nothing but passing the "ListName" property that we created in Step 2 as parameter to the Gridview.
7) Now, go back to the webpart file "MyVisualWP.cs" and override the "CreateEditorParts()" method which returns an EditorPartCollection. In this method we will add our custom EditorPart "WPEditor.cs" to the EditorPartCollection.
8) Following is the completed code for "MyVisualWP.ascx.cs" visual webpart class file.
9) Deploy the solution and add the webpart to the page in edit mode. You can see that there is a dropdownlist in the webpart properties window with all the lists. Choose any of the list and click 'Apply' or 'Ok' button.
10) The Gridview will be populated with the items in the list that you selected in the dropdownlist. Similarly, you can choose any other list from the drop down to populate data.
So that was all about custom EditorParts in webparts in SharePoint 2013!
In the previous post we discussed how we can create configurable webpart properties. We also noticed that the web part properties were ENUM and we could only enter static values as parameter like typing a list name or column name etc.
What if you type the list name incorrectly? Therefore, you would want to choose a list from a asp.net dropdown containing all the available lists and display the items for the selected list in a Gridview. This is were EditorParts come in handy! So lets get started...
Editor Parts
To customize the Web Parts’ configuration user interface you can create custom classes called Editor Parts which are controls hosted in a specific WebPartZone called EditorZone.
ASP.NET 4.0 contains an abstract class called EditorPart, this class is used with the ASP.NET 4.0 WebPart class to create customized tool panes. By inheriting from this control you can customize the look and functionality of the tool pane and use standard ASP.NET constructs such as auto post backs, validations etc.
IWebEditable Interface
To provide a Web Part with a custom Editor Part, you need to override the implementation of the IWebEditable interface, which is implemented by the Web Part base class.
Steps to create an Editor Part
Following are the steps:-
1) Create a Visual web part as mentioned in the the post "Creating a Visual Webpart".
2) Go to the webpart file "MyVisualWP.ascx.cs" and implement the "IWebEditable" interface. Also, add a property named "ListName" as shown in the figure below.
3) Now add a new class named "WPEditor.cs" which inherits from the "EditorPart" class. Create a new Dropdownlist and populate the list with all the Lists in the site. Then add the dropdownlist to the controls collection as shown in the figure below.
4) EditorPart class has 2 important methods:
i) Apply Changes() : This method is used to save any changes to the currently-edited Web Part.
ii) SyncChanges() : This method is used to load the current configuration from Web Part Properties.
5) Following is the complete code "WPEditor.cs" class file.
Code Snippet
- using Microsoft.SharePoint;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- namespace MyVisualWebpart.MyVisualWP
- {
- public class WPEditor : EditorPart
- {
- private DropDownList ddlList;
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- ddlList = new DropDownList();
- SPWeb oWeb = SPContext.Current.Web;
- foreach (SPList oLists in oWeb.Lists)
- {
- ddlList.Items.Add(oLists.Title);
- }
- }
- protected override void CreateChildControls()
- {
- base.CreateChildControls();
- this.Controls.Add(new LiteralControl("My List Name<br/>"));
- this.Controls.Add(ddlList);
- this.Controls.Add(new LiteralControl("<br/>"));
- }
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- }
- public override bool ApplyChanges()
- {
- MyVisualWP webPart = this.WebPartToEdit as MyVisualWP;
- if (webPart != null)
- {
- webPart.ListName = ddlList.SelectedValue;
- }
- return true;
- }
- public override void SyncChanges()
- {
- MyVisualWP webPart = this.WebPartToEdit as MyVisualWP;
- if (webPart != null)
- {
- ddlList.SelectedValue = webPart.ListName;
- }
- }
- }
- }
6) Make the following changes in the user controls code behind file "MyVisualWPUserControl.ascx.cs" which nothing but passing the "ListName" property that we created in Step 2 as parameter to the Gridview.
7) Now, go back to the webpart file "MyVisualWP.cs" and override the "CreateEditorParts()" method which returns an EditorPartCollection. In this method we will add our custom EditorPart "WPEditor.cs" to the EditorPartCollection.
8) Following is the completed code for "MyVisualWP.ascx.cs" visual webpart class file.
Code Snippet
- using System;
- using System.ComponentModel;
- using System.Web.UI.WebControls.WebParts;
- using Microsoft.SharePoint;
- using System.Collections.Generic;
- namespace MyVisualWebpart.MyVisualWP
- {
- [ToolboxItemAttribute(false)]
- public partial class MyVisualWP : WebPart, IWebEditable
- {
- public string ListName { get; set; }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (this.ListName != null && this.ListName.Trim() != "")
- {
- SPSite oSite = SPContext.Current.Site;
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- SPList oList = oWeb.Lists[this.ListName];
- SPListItemCollection oColl = oList.GetItems();
- gvwProducts.DataSource = oColl.GetDataTable();
- gvwProducts.DataBind();
- }
- }
- }
- public override EditorPartCollection CreateEditorParts()
- {
- List<EditorPart> editorParts = new List<EditorPart>();
- WPEditor oWPEditor = new WPEditor();
- oWPEditor.ID = this.ID + "_sampleEditorPart";
- oWPEditor.Title = "Personalize";
- editorParts.Add(oWPEditor);
- return new EditorPartCollection(base.CreateEditorParts(), editorParts);
- }
- public MyVisualWP()
- {
- }
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- InitializeControl();
- }
- }
- }
9) Deploy the solution and add the webpart to the page in edit mode. You can see that there is a dropdownlist in the webpart properties window with all the lists. Choose any of the list and click 'Apply' or 'Ok' button.
10) The Gridview will be populated with the items in the list that you selected in the dropdownlist. Similarly, you can choose any other list from the drop down to populate data.
So that was all about custom EditorParts in webparts in SharePoint 2013!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.