从SPFieldCalculated派生的自定义字段类型

时间:2009-05-21 20:56:28

标签: sharepoint sharepoint-2007 moss

我正在尝试创建一个自定义字段类型,它将根据同一列表中的另一个字段显示值。 (基本上,根据源列的值显示一些图像)

由于此列完全基于另一列派生它的数据,我需要确保列值不存储在数据库中。

所以,我认为我需要创建一个派生自SPFieldCalculated类的自定义字段类型。

但是,当我尝试这样做时,我收到一条错误消息 “类型'Microsoft.SharePoint.SPFieldCalculated'没有定义构造函数”

如何创建源自SPFieldCalculated的自定义字段? 如果这是不可能的,至少有人能告诉我如何满足我的要求吗?

6 个答案:

答案 0 :(得分:2)

在fieltype xml中,您可以在模板的CAML中添加LookupColumn。 在您的问题中,您可以使用字段类型文本,并在DisplayPattern中写下这样的内容。

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

答案 1 :(得分:2)

不幸的是,无法从SPCalculatedField类继承,这就是您收到该错误的原因: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcalculated.aspx

  

Windows SharePoint Services 3.0可以   不支持从这继承   类。

为了在2007年根据另一列的值显示图像,你将不得不在fldtypes ... xml中做一些令人不快的CAML工作(如@David A提及)。这是一个很好的起点: http://msdn.microsoft.com/en-us/library/aa544291.aspx

我也会无辜地提到可能有这种功能的商业替代品,其中一个我可能帮助创建,可能在这里提到: Sharepoint webpart for conditional formatting?

答案 2 :(得分:1)

SPField没有你想要的SPFieldCalculated吗?我会通过基于SPField创建自定义字段来解决问题,然后我会:

1在字段编辑器中注册事件接收器,并使用它来设置字段的值。

2如果要在编辑/新表单中预览值,请在字段控件中计算值。这要求您的字段在基于您的值的字段下方排序,否则您将最终根据旧数据计算您的值。

如果重新计算,我还建议您保存计算值。如果要在列表视图中显示数据,这是唯一合理的方法,因为您在CAML中编写渲染模式,而不是C#。

答案 3 :(得分:1)

实际上,您可以为列表创建custom view style获得更多里程。

这将允许您完全更改列以html方式显示的方式,而无需添加计算字段。

Alternativley您还可以考虑在schemaxml中使用自定义字段定义可以做一些非常时髦的事情。作为一个例子,我为普通发布页面库中的一个“名称”字段包含了模式xml。

<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>

讨厌,但有力。

答案 4 :(得分:0)

我很害怕我之前没有尝试制作自定义字段..但是我有一个可能有效的建议。该字段可以只是一个图片字段。您可以在列表中使用事件接收器,只要目标字段发生更改,就会计算所需的字段。有点啰嗦的方式,但如果你没有得到自定义字段的工作方式,这可能会。

答案 5 :(得分:0)

假设这个专栏真的只是为了展示,我认为有两种方法可能有所帮助:

  1. 使用计算列创建HTML,然后使用JavaScript将其“打开”:http://pathtosharepoint.wordpress.com/2008/09/01/apply-color-coding-to-your-sharepoint-lists/
  2. 您可以使用SharePoint Designer和DataForm webpart执行此操作:http://masteringsharepoint.com/blogs/beagle/archive/2008/08/13/DFWP1.aspx