Friday 11 July 2014

Create a Custom Web Part properties for SharePoint 2010

Create a Custom Web Part properties for SharePoint 2010
Add dropdown in web part properties
Open VS 2010
File New SharePoint project
Select Visual Web Part
Name it: MyWebPart
Add below code
MyWebPart.cs
---------------
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;
namespace MyWebPartProject.MyWebPart
{
[ToolboxItemAttribute(false)]
public class MyWebPart : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/MyWebPartProject/MyWebPart/MyWebPartUserControl.ascx";
public string _OptionValue { get; set; }
public enum ddlEnum { option1, option2, option3 }
[WebBrowsable(true),
Category("Miscellaneous"),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("Dropdown List Display Text")]
public ddlEnum ddlProp { get; set; }
//[WebBrowsable(true),
//Category("Miscellaneous"),
//Personalizable(PersonalizationScope.Shared),
//WebDisplayName("Enter some text")]
//public string CustomTextProp { get; set; }
protected override void CreateChildControls()
{
MyWebPartUserControl control = Page.LoadControl(_ascxPath) as MyWebPartUserControl;
if (control != null)
control.WebPart = this;
Controls.Add(control);
}
}
public class MyEditorPart : EditorPart
{
private Label lbl_Options;
private DropDownList ddl_Options;
protected override void CreateChildControls()
{
base.CreateChildControls();
ddl_Options = new DropDownList();
lbl_Options = new Label();
lbl_Options.Text = "<strong>Choose:</strong><br />:";
using (SPSite site = new SPSite("http://" + Page.Request.Url.Host.ToString()))
{
using (SPWeb web = site.OpenWeb())
{
SPList Forms = web.Lists["Side Panel Content"];
SPQuery qry = new SPQuery();
qry.Query = @"<Query><Where><Gt><FieldRef Name=’ID’ /><Value Type=’Counter’>0</Value></Gt></Where></Query>";
SPListItemCollection SPLIC = Forms.GetItems(qry);
if (SPLIC != null && SPLIC.Count > 0)
{
foreach (SPListItem SPLI in SPLIC)
{
string OptionTitle = "";
string OptionID = "";
if (SPLI["ID"] != null)
OptionID = SPLI["ID"].ToString();
if (SPLI["Title"] != null)
OptionTitle = SPLI["Title"].ToString();
if (OptionTitle != "" && OptionID != "")
ddl_Options.Items.Add(new ListItem(OptionTitle, OptionID));
}
}
}
}
Controls.Add(lbl_Options);
Controls.Add(ddl_Options);
}
public override bool ApplyChanges()
{
EnsureChildControls();
MyWebPart webPart = WebPartToEdit as MyWebPart;
if (webPart != null)
webPart._OptionValue = ddl_Options.SelectedValue.ToString();
return true;
}
public override void SyncChanges()
{
EnsureChildControls();
MyWebPart webPart = WebPartToEdit as MyWebPart;
if (webPart != null)
{
ddl_Options.SelectedValue = webPart._OptionValue.ToString();
}
}
}
}
===========================================================================
MyWebPartUserControl.ascx
-----------------------------------
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyWebPartUserControl.ascx.cs" Inherits="MyWebPartProject.MyWebPart.MyWebPartUserControl" %>

<asp:Label ID="lbl_MySelection" runat="server" Text="Label"></asp:Label>

MyWebPartUserControl.ascx.cs
--------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace MyWebPartProject.MyWebPart
{
public partial class MyWebPartUserControl : UserControl
{
public MyWebPart WebPart { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (this.WebPart != null && this.WebPart.ddlProp != null)
//lbl_MySelection.Text = WebPart._OptionValue;
lbl_MySelection.Text = WebPart.ddlProp.ToString();
//if (this.WebPart != null && this.WebPart.CustomTextProp != null)
//{
// lbl_MySelection.Text = this.WebPart.CustomTextProp.ToString();
//}
}
}
}