Creating a custom column part 2

Must have TWO cs files, or at least two classes, i like to separate them into separate files. The main CS is really just setting the stage. All the heavy lifting is done in the second class.

MAIN CS

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KMS_Custom_column_Systems.FeatureCode
{
class KMS_CustomColumn_systems_List_main : SPFieldText
{
public KMS_CustomColumn_systems_List_main(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{}

public KMS_CustomColumn_systems_List_main(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{}

public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new KMS_CustomColumn_systems_List_worker(this);

fieldControl.FieldName = InternalName;

return fieldControl;
}
}
}
}

WORKER Class

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace KMS_Custom_column_Systems.FeatureCode
{
class KMS_CustomColumn_systems_List_worker : TextField
{
private KMS_CustomColumn_systems_List_main field;
private HtmlTable table;
private DropDownList dropdownlist;

public KMS_CustomColumn_systems_List_worker(KMS_CustomColumn_systems_List_main parentField)
{
this.field = parentField;
this.dropdownlist = new DropDownList();
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

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

this.table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
table.Rows.Add(row);

HtmlTableCell cell = null;

if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
{

cell = new HtmlTableCell();
row.Cells.Add(cell);

// Create a list selector.
this.dropdownlist = new DropDownList();
using (SPWeb rootWeb = SPContext.Current.Site.RootWeb)
{
dropdownlist.Items.Add(“Select One”);
foreach(SPWeb subsite in rootWeb.Webs)
{
if (subsite.Name.ToString() == “systems”)
{
foreach (SPWeb newsubsite in subsite.Webs)
{
dropdownlist.Items.Add(new ListItem(newsubsite.Name, newsubsite.Name.ToString()));
subsite.Dispose();
}
}

}
}

// Get the current value of the field.
String currentValue = (String)this.ItemFieldValue;

if (currentValue != null && currentValue != String.Empty)
{
this.dropdownlist.SelectedValue = currentValue;
}
else if (this.dropdownlist.Items.Count > 0)
{
this.dropdownlist.SelectedIndex = 0;
}

// Add the script which updates the preview image.
this.dropdownlist.Attributes[“onchange”] = “this.options[this.selectedIndex].value;”;
cell.Controls.Add(this.dropdownlist);
row.Cells.Add(cell);
}

cell = new HtmlTableCell();

LiteralControl literalControl = new LiteralControl();

String logo = null;
object logoObject = this.ItemFieldValue;

if (logoObject != null)
{
logo = (String)logoObject;
}

literalControl.Text =  logo;

cell.Controls.Add(literalControl);

row.Cells.Add(cell);

base.Controls.Add(table);
}
public override void UpdateFieldValueInItem()
{
this.EnsureChildControls();

try
{
this.Value = this.dropdownlist.SelectedValue;
this.ItemFieldValue = this.Value;
}
catch (Exception)
{
;
}
}
protected override void Render(HtmlTextWriter output)
{
this.table.RenderControl(output);
}
}
}

Comments are closed.