Scenario: Create a custom choice field which will have all the calendars of the current website as its field values.
Create an empty SharePoint 2010 project and deploy it as a farm solution. Add SharePoint mapped folder i.e, CONTROLTEMPLATES folder. Following is the solution structure:
Add the user control, delete the code behind files and insert the following markup in the ascx file.
Create a Fields folder and add two class files inside it as shown in the screenshot.
Use the following code in CalendarChoiceField.cs file:
Create an empty SharePoint 2010 project and deploy it as a farm solution. Add SharePoint mapped folder i.e, CONTROLTEMPLATES folder. Following is the solution structure:
<%@ 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" %>
<SharePoint:RenderingTemplate ID="DropDownRenderingTemplate" runat="server">
<Template>
<asp:DropDownList ID="calendarDropDown" runat="server"></asp:DropDownList>
</Template>
</SharePoint:RenderingTemplate>
Create a Fields folder and add two class files inside it as shown in the screenshot.
Use the following code in CalendarChoiceField.cs file:
namespace NY.CalendarChoice.Fields
{
class CalendarChoiceField : SPFieldChoice
{
#region Constructors
public CalendarChoiceField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
public CalendarChoiceField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { }
#endregion
#region Properties
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new CalendarChoiceFieldControl();
fieldControl.FieldName = InternalName;
return fieldControl;
}
}
#endregion
}
}
Use the following code in CalendarChoiceFieldControl.cs file
namespace NY.CalendarChoice.Fields
{
class CalendarChoiceFieldControl : BaseFieldControl
{
DropDownList calendarDropDown;
protected override string DefaultTemplateName
{
get
{
return "DropDownRenderingTemplate";
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
calendarDropDown = (DropDownList)TemplateContainer.FindControl("calendarDropDown");
if (ControlMode == SPControlMode.New || ControlMode == SPControlMode.Edit)
{
SPWeb currentWeb = SPContext.Current.Web;
SPListCollection lists = currentWeb.Lists;
foreach (SPList list in lists)
{
if (list.BaseTemplate == SPListTemplateType.Events)
{
calendarDropDown.Items.Add(list.Title);
}
}
}
}
public override object Value
{
get
{
EnsureChildControls();
return calendarDropDown.SelectedValue;
}
set
{
this.EnsureChildControls();
calendarDropDown.SelectedValue = (string)ItemFieldValue;
}
}
}
}