Monday, September 6, 2010

Adding custom field into sharepoint

Hi,
First of all thanx to SharePointKings site
First in my Control.ascx i have written as below and had put in 12hive\TEMPLATE\CONTROLTEMPLATES path
<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>



After that in my fldtypes_control.xml I have written as below and had put in 12hive\TEMPLATE\XML



SPKings Textbox
SPKings Textbox
SPKings Textbox
Text
SharePointKings.CustomControls.SPKingsTextBoxField, SharePointKings.CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0dcfd7c46d44c992
TRUE
TRUE


After that in SPKingsTextboxControl.cs I have written as follows
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
namespace SharePointKings.CustomControls
{
public class SPKingsTextboxControl:BaseFieldControl
{
protected TextBox txtTextBox;
///
/// setting template name
///

protected override string DefaultTemplateName
{
get
{
return @"SPKingsTextbox";
}
}

///
/// get set value
///

public override object Value
{
get
{
EnsureChildControls();
return txtTextBox.Text.Trim();
}

set
{
EnsureChildControls();
txtTextBox.Text = (string)this.ItemFieldValue;
}
}
///
/// create child control
///

protected override void CreateChildControls()
{
if (Field == null) return;
base.CreateChildControls();
}

}
}

After that in SPKingsTextBoxField.cs I have written as
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SharePointKings.CustomControls
{
class SPKingsTextBoxField:SPFieldText
{
#region Contstructors
public SPKingsTextBoxField(SPFieldCollection fields, string fieldName): base(fields, fieldName)
{
}
public SPKingsTextBoxField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName)
{

}

#endregion
public override string GetValidatedString(object value)
{
string strTxtValue = Convert.ToString(value).Trim();

if (!strTxtValue.ToUpper().StartsWith("HTTP") &&
!strTxtValue.ToUpper().StartsWith("FTP"))
{
throw new SPFieldValidationException("Web address does not start with HTTP or FTP");
}
return strTxtValue;
}
}
}

No comments:

Post a Comment