views:

1554

answers:

5

I am trying to create a custom field type which will display values based on another field in the same list. (basically, display some images based on the value of the source column)

Since this column derives it's data entirely on the basis of another column, i need to make sure the column values are not stored in the database.

So, i think i need to create a custom field type which derives from SPFieldCalculated class.

But, when i try to do so, I get an error message "The type 'Microsoft.SharePoint.SPFieldCalculated' has no constructors defined"

How do i create a custom field tpye deriving from SPFieldCalculated? If this is not possible, can somebody at least tell me how do i go about satisfying my requirement?

A: 

Well afraid I've not tried making custom fields before.. however I have a suggestion that might work. The field could be simply a picture field. You could use an event receiver on the list that calculates the required field whenever the target field changes. Bit of a long winded way round but if you don't get the custom field way to work, this might.

Dan Revell
A: 

Assuming this column is really only meant to be displayed there are two ways I can think of that might help:

  1. Use the calculated column to create HTML and then JavaScript to "turn it on": http://pathtosharepoint.wordpress.com/2008/09/01/apply-color-coding-to-your-sharepoint-lists/
  2. You could probably do this with SharePoint Designer and the DataForm webpart: http://masteringsharepoint.com/blogs/beagle/archive/2008/08/13/DFWP1.aspx
Kit Menke
A: 

Is there anything you want from SPFieldCalculated that SPField doesn’t have? I would solve the problem by making a custom field based on SPField, then I would either:

1 Register an event receiver in the field editor and use it to set the value of the field.

or

2 If you want to preview the value in edit/new forms, calculate the value in the field control. This requires your field to be ordered below the fields you base your value on, otherwise you will end up calculating your value based on old data.

I would also recommend you to save your calculated value instead if recalculating it. This is the only reasonable way if you want to display the data in a lists view, since you write the render pattern in CAML, not C#.

JMD
+1  A: 

You may actually get more mileage from creating a custom view style for the list(s).

This would allow you to completely change the way the column displays html-wise, without needing to add a calculated field.

Alternativley you could also consider that it is possible to do some very funky things with a custom field definition just in the schemaxml. As an example I include the schema xml for one of the "name" fields in an ordinary publishing pages library.

<Field ID="{9d30f126-ba48-446b-b8f9-83745f322ebe}" ReadOnly="TRUE" Type="Computed" Name="LinkFilenameNoMenu" DisplayName="Name" DisplayNameSrcField="FileLeafRef" Filterable="FALSE" AuthoringInfo="(linked to document)" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="LinkFilenameNoMenu" FromBaseType="TRUE"><FieldRefs><FieldRef Name="FileLeafRef" /><FieldRef Name="FSObjType" /><FieldRef Name="Created_x0020_Date" /><FieldRef Name="FileRef" /><FieldRef Name="File_x0020_Type" /><FieldRef Name="ContentTypeId" /><FieldRef Name="PermMask" /><FieldRef Name="CheckoutUser" /><FieldRef Name="IsCheckedoutToLocal" /></FieldRefs><DisplayPattern><IfEqual><Expr1><LookupColumn Name="FSObjType" /></Expr1><Expr2>1</Expr2><Then><FieldSwitch><Expr><GetVar Name="RecursiveView" /></Expr><Case Value="1"><LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" /></Case><Default><SetVar Name="UnencodedFilterLink"><SetVar Name="RootFolder"><HTML>/</HTML><LookupColumn Name="FileRef" /></SetVar><SetVar Name="FolderCTID"><FieldSwitch><Expr><ListProperty Select="EnableContentTypes" /></Expr><Case Value="1"><Column Name="ContentTypeId" /></Case></FieldSwitch></SetVar><FilterLink Default="" Paged="FALSE" /></SetVar><HTML><![CDATA[<A onfocus="OnLink(this)" HREF="]]></HTML><GetVar Name="UnencodedFilterLink" HTMLEncode="TRUE" /><HTML><![CDATA[" onclick="javascript:EnterFolder(']]></HTML><ScriptQuote NotAddingQuote="TRUE"><GetVar Name="UnencodedFilterLink" /></ScriptQuote><HTML><![CDATA[');return false;">]]></HTML><LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" /><IfEqual><Expr1><GetVar Name="ShowAccessibleIcon" /></Expr1><Expr2>1</Expr2><Then><HTML><![CDATA[<img src="/_layouts/images/blank.gif" class="ms-hidden" border=0 width=1 height=1 alt="]]></HTML><HTML>Use SHIFT+ENTER to open the menu (new window).</HTML><HTML><![CDATA[">]]></HTML></Then></IfEqual><HTML><![CDATA[</A>]]></HTML></Default></FieldSwitch></Then><Else><HTML><![CDATA[<A onfocus="OnLink(this)" HREF="]]></HTML><Field Name="ServerUrl" URLEncodeAsURL="TRUE" /><HTML><![CDATA[" onclick="return DispEx(this,event,']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ServerProperty Select="HtmlTransform" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ServerProperty Select="HtmlTrAcceptType"><Column Name="File_x0020_Type" /></ServerProperty></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ServerProperty Select="HtmlTrHandleUrl"><Column Name="File_x0020_Type" /></ServerProperty></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ServerProperty Select="HtmlTrProgId"><Column Name="File_x0020_Type" /></ServerProperty></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ListProperty Select="DefaultItemOpen" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><MapToControl><Column Name="HTML_x0020_File_x0020_Type" /><HTML>|</HTML><Column Name="File_x0020_Type" /></MapToControl></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><Column Name="HTML_x0020_File_x0020_Type" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ServerProperty Select="GetServerFileRedirect"><Field Name="ServerUrl" /><HTML>|</HTML><Column Name="HTML_x0020_File_x0020_Type" /></ServerProperty></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><Column Name="CheckoutUser" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><UserID AllowAnonymous="TRUE" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><ListProperty Select="ForceCheckout" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><Field Name="IsCheckedoutToLocal" /></ScriptQuote><HTML><![CDATA[',']]></HTML><ScriptQuote NotAddingQuote="TRUE"><Field Name="PermMask" /></ScriptQuote><HTML><![CDATA[')">]]></HTML><UrlBaseName HTMLEncode="TRUE"><LookupColumn Name="FileLeafRef" /></UrlBaseName><IfEqual><Expr1><GetVar Name="ShowAccessibleIcon" /></Expr1><Expr2>1</Expr2><Then><HTML><![CDATA[<img src="/_layouts/images/blank.gif" class="ms-hidden" border=0 width=1 height=1 alt="]]></HTML><HTML>Use SHIFT+ENTER to open the menu (new window).</HTML><HTML><![CDATA[">]]></HTML></Then></IfEqual><HTML><![CDATA[</A>]]></HTML><IfNew Name="Created_x0020_Date"><HTML><![CDATA[<IMG SRC="/_layouts/1033/images/new.gif" alt="]]></HTML><HTML>New</HTML><HTML><![CDATA[">]]></HTML></IfNew></Else></IfEqual></DisplayPattern></Field>

Nasty, but potent.

Nat
A: 

In the fieltype xml you can add in the CAML of the template a LookupColumn. in you question you can use a field type text and in the DisplayPattern write something like this.

<RenderPattern Name="DisplayPattern">
    <HTML><![CDATA[<img src="]]></HTML>
      <LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" />
    <HTML><![CDATA["/>]]></HTML>
</RenderPattern>
David A