home screen

Search



Number Of Result : 0

Result :


Thursday, April 15, 2010

Create a new custom type - SharePoint

- Create a project as follow:



Notice:(if you don't use WSPBuilder)
1/ put your MyNewTypeControl.ascx @ rootdir:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES
2/ put your xml file @ rootdir:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML
3/ put your dll in the GAC @ rootdir:\WINDOWS\assembly

- MyNewTypeControl.ascx

<%@ Control Language="C#" Debug="true" %>
<%@ 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" %>
<SharePoint:RenderingTemplate ID="MyFieldTypeListBoxRenderingTemplate" runat="server">
<Template>
<asp:ListBox ID="MyFieldTypeListControl" runat="server" />
</Template>
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="MyFieldTypeRadioButtonListRenderingTemplate" runat="server">
<Template>
<asp:RadioButtonList ID="MyFieldTypeListControl" runat="server" />
</Template>
</SharePoint:RenderingTemplate>


- MyNewTypeControl.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.HtmlControls;
using System.Web.UI.WebControls;


namespace WSPBuilderProject1
{
public class MyNewTypeControl : BaseFieldControl
{
protected ListControl myFieldTypeListControl;
protected override string DefaultTemplateName
{
get
{
string defaultTemplateName = "MyFieldTypeRadioButtonListRenderingTemplate";
MyNewTypeField tfield = this.Field as MyNewTypeField;
if (tfield.UseListBox)
defaultTemplateName = "MyFieldTypeListBoxRenderingTemplate";
return defaultTemplateName;
}
}
public override object Value
{
get
{
this.EnsureChildControls();
return this.myFieldTypeListControl.SelectedValue;
}
set
{
EnsureChildControls();
this.myFieldTypeListControl.SelectedValue = (string)this.ItemFieldValue;
}
}
protected override void CreateChildControls()
{
if (this.Field == null || this.ControlMode == SPControlMode.Display)
return;
base.CreateChildControls();
this.myFieldTypeListControl =
(ListControl)TemplateContainer.FindControl("MyFieldTypeListControl");
if (this.myFieldTypeListControl == null)
throw new Exception("The user control does not contain the ListControl!");
if (!this.Page.IsPostBack)
{
MyNewTypeField tfield = this.Field as MyNewTypeField;
string str = tfield.AvailableItems;
string[] items = str.Split(';');
string[] texts = new string[items.Length];
string[] values = new string[items.Length];
for (int i = 0; i {
string[] str2 = items[i].Split(',');
texts[i] = str2[0];
values[i] = str2[1];
}
for (int j = 0; j {
this.myFieldTypeListControl.Items.Add(new ListItem(texts[j], values[j]));
}
}
}
}
}





- MyNewTypeField.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 WSPBuilderProject1
{
public class MyNewTypeField : SPFieldText
{
public MyNewTypeField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName) { }
public MyNewTypeField(SPFieldCollection fields, string typeName,
string displayName)
: base(fields, typeName, displayName) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl control = new MyNewTypeControl();
control.FieldName = this.InternalName;
return control;
}
}
public bool UseListBox
{
get
{
return (bool)this.GetCustomProperty("UseListBox");
}
set
{
this.SetCustomProperty("UseListBox", value);
}
}
public string AvailableItems
{
get
{
return (string)this.GetCustomProperty("AvailableItems");
}
set
{
this.SetCustomProperty("AvailableItems", value);
}
}
}
}



- fldtypes_MyCustomField.xml

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">MyNewType</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">My New Field Type</Field>
<Field Name="TypeShortDescription">This is My Field Type</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="AllowBaseTypeRendering">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
<Field Name="FieldTypeClass">WSPBuilderProject1.MyNewTypeField, WSPBuilderProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98aaedbbadfe636f</Field>
<Field Name=""></Field>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr>
<Column/>
</Expr>
<Case Value="">
</Case>
<Default>
<HTML>
<![CDATA[<span style="background-color:blue;color:yellow;font-bold:true">]]>
</HTML>
<Column HTMLEncode="TRUE"/>
<HTML>
<![CDATA[</span>]]>
</HTML>
</Default>
</Switch>
</RenderPattern>
<PropertySchema>
<Fields>
<Field Name="AvailableItems"
DisplayName="Available Items"
MaxLength="200"
DisplaySize="100"
Type="Text"/>
<Field Name="UseListBox"
DisplayName="Use ListBox"
MaxLength="200"
DisplaySize="100"
Type="Boolean"/>
</Fields>
</PropertySchema>
</FieldType>
</FieldTypes>


No comments: