Sharepoint solutions

April 29, 2009

Custom computed field in the display list form

Filed under: main — admin @ 4:02 pm

Sharepoint  provide two field type that can calculate value upon some formula to show information in the display list form – calculated field type and computed field type. Calculated field store its value in the database and update this value  only when list item has been updated. So, this field type can not be used to show data that can be changed independently of the list item. Computed field can only calculate formula that conists only other fields from list item. We can not derive custom field from spfieldcomputed and can only use caml to make this formula in the scheme.
  My task was – show in the list display form (dispform.aspx usually, but actually this name is set in the list scheme) some data that depends of the list item  and do not depends of it (in the list item1 there was referrence field refid to id of another item2 and I must show  transformed upon some fomula data from item2 – its name to uppercase for example).

Brief description of the solution:

To resolve this task I create new custom field derived from SPFieldText and overwrite
RenderFieldForDisplay method of control class that render this field.

Details description of the solution:

1. Make new visual studio project of type class library and create new class:

     // example of creating a custom field type
    public class SomeCustomField : SPFieldText
    {

        public SomeCustomField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName) { }

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

        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {
            get
            {
               
                BaseFieldControl control = new SomeCustomFieldControl();
                control.FieldName = this.InternalName;
                return control;
            }
        }
    }

    // custom field type uses helper class to initialize and render control
    public class SomeCustomFieldControl : BaseFieldControl
    {
            
        protected override string DefaultTemplateName
        {
            get
            {
                return @”SomeCustomFieldControl”;
            }
        }
       
        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {

            int IdOfItem2 = (int)this.ListItem["refid"];
            SPListItem item2 = this.ListItem.ParentList.GetItemById(IdOfItem2);
            string data = item2.Name.ToUpper();
            output.Write(data);
            base.RenderFieldForDisplay(output);
        }
  

    }
2.Compile this class library, make assembly, give it strong name and install it to GAC.
(How making all things are easy in sharepoint! ;) )

3.Make SomeCustomField.ascx with the following content:
<SharePoint:RenderingTemplate ID=”SomeCustomFieldControl” runat=”server”>
  <Template>
 
  </Template>
</SharePoint:RenderingTemplate>

Copy this file to the Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory
3.Make fldtypes_SomeCustomField.xml with the following content:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<FieldTypes>
 

  <FieldType>
    <Field Name=”TypeName”>SomeCustomField</Field>
    <Field Name=”ParentType”>Text</Field>
    <Field Name=”TypeDisplayName”>SomeCustomField Status</Field>
    <Field Name=”TypeShortDescription”>SomeCustomField Status Field</Field>
    <Field Name=”UserCreatable”>TRUE</Field>
    <Field Name=”ShowInListCreate”>TRUE</Field>
    <Field Name=”ShowInSurveyCreate”>TRUE</Field>
    <Field Name=”ShowInDocumentLibraryCreate”>TRUE</Field>
    <Field Name=”ShowInColumnTemplateCreate”>TRUE</Field>
    <Field Name=”FieldTypeClass”>SomeCustomFieldAssembly.SomeCustomField, SomeCustomField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11ae1111111ddd11</Field>
    <RenderPattern Name=”DisplayPattern”>
      <Switch>
        <Expr>
          <Column/>
        </Expr>
        <Case Value=”">
        </Case>
        <Default>
        
          <Column SubColumnNumber=”0″ HTMLEncode=”TRUE”/>
        
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
 
 
</FieldTypes>

Copy this file to the Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML

4. Install and activate feature.xml:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<!– _lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>
<!– _LocalBinding –>
<Feature  Id=”11111111-1111-1111-85B9-5ED1D39C073B”
  Title=”CustomField”
  Description=”SomeCustomField Type”
  Version=”12.0.0.0″
  Scope=”Site”
  xmlns=”http://schemas.microsoft.com/sharepoint/” >
  <ElementManifests>
    <ElementManifest Location=”elements.xml”/>
  </ElementManifests>
</Feature>

where the elements.xml is:

 <Field ID=”{CA1A1D11-2123-4db7-B11A-1C0D05D01111}”
            Name=”SomeCustomFieldDerived”
            Group=”Custom”
           Type=”SomeCustomField”
           DisplayName=”SomeCustomField Derived”
           ShowInDisplayForm=”TRUE”
            Sortable=”FALSE”
            ReadOnly=”TRUE”
            Required=”false”    
            Hidden=”false”
      SourceID=”http://schemas.microsoft.com/sharepoint/v3
      StaticName=”SomeCustomFieldDerived”
     
      />
 
 That’s all now we can add this field type to our content type or list.

We can not make caml query to search this field and the real value of this field is null, so in the allitems.aspx listing the value will be also null.

2 Comments

  1. I found a problem with computed
    fields read my blog entry to this:

    http://www.schonebeck.net/?p=43

    Comment by David — May 7, 2010 @ 12:56 am

  2. I learn something important from this post. That s way i want to thank you .

    Comment by soft-7.com — June 19, 2010 @ 6:46 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress