Home > SharePoint > Custom Lookup Fields

Custom Lookup Fields

I was working on a client project recently and was using the out of the box SharePoint Lookup fields to manage ‘admin lists’ Basically allowing the user’s to manage the contents of their own drop down list fields.

But the problem with the out of the box look up field is that it displays the value in the list views and when reading an  item as a Hyperlink. So when the user click the link they get taken to the linked item, which in this case, is just an admin list with one field in it. So they kind of get lost!

This doesn’t improve usability of the application so I did some investigating and came up with the following solution. If anyone has any other ideas on how to achieve this I’d love to hear from you.

The following screen shots illustrate the solution you can see the lookup field is now display without the hyperlink to the corresponding lookup list, this is also the case when the list item is displayed in read mode.

image 

 

Field being displayed without the hyperlink.

image 

image

 

After looking at the FLYTYPES.XML file found in the …12\TEMPLATE\XML directory of SharePoint I located the ‘Lookup’ field definition…

<Field Name="TypeName">Lookup</Field>

If you look at it’s RenderTemplate and in particular the ‘DisplayPattern’ you will see this is where the anchor tag is added.

So, my solution was to create your own custom field  which has a parent type of Lookup and then define your own RenderPattern without any anchor tags.

You can do this by using the visual studio extensions for SharePoint. After creating a new solution add a new Field Control

 

image

Then in the field definition I defined the following field….

 

<FieldType>
  <Field Name="TypeName">LookupNoLinkField</Field>
  <Field Name="TypeDisplayName">LookupNoLinkField</Field>
  <Field Name="TypeShortDescription">Lookup (No Link)</Field>
  <Field Name="ParentType">Lookup</Field>
  <Field Name="UserCreatable">FALSE</Field>
  <Field Name="FieldTypeClass">7710ed2c-00e2-40e8-92dc-ea8ee5197138</Field>
  <Field Name="FieldEditorUserControl">/_controltemplates/LookupFieldEditor.ascx</Field>
  <Field Name="SQLType">int</Field>
  <Field Name="Sortable">TRUE</Field>
  <Field Name="Filterable">TRUE</Field>
  <Field Name="ShowInListCreate">TRUE</Field>
  <RenderPattern Name="DisplayPattern">
    <LookupColumn HTMLEncode="TRUE" />
  </RenderPattern>
</FieldType>

Ok so because nothing in life is easy, there is a nice Gotcha! If either through the UI or through the API design to change this field now to allow multiv values then what actually happens is the field type is change to LookupMulti. So back comes your anchor tags. So you need to do the same thing but this time base you new custom field to parent the LookupMulti type.

<FieldType>

  <
Field Name="TypeName">LookupNoLinkMultiField</Field>

  <
Field Name="TypeDisplayName">LookupNoLinkMultiField</Field>

  <
Field Name="TypeShortDescription">Lookup-Multi (No Link)</Field>

  <
Field Name="ParentType">LookupMulti</Field>

  <
Field Name="UserCreatable">FALSE</Field>

  <
Field Name="FieldTypeClass">63aa539e-d34e-4858-bb93-c659103713f1</Field>

  <
Field Name="FieldEditorUserControl">/_controltemplates/LookupFieldEditor.ascx</Field>

  <
Field Name="SQLType">int</Field>

  <
Field Name="Sortable">TRUE</Field>

  <
Field Name="Filterable">TRUE</Field>

  <
RenderPattern Name="DisplayPattern">

    <
LookupColumn HTMLEncode="TRUE" />

  </
RenderPattern>

</
FieldType>

 

With the solution I was building I was changing the field type to allow multi values…

lookupField.AllowMultipleValues =

true;

this resulted in the field reverting to the standard lookup field types. So how do you change this to be your customer lookup field.

Well to begin with I looked at the method SPField.Type which takes an enumeration value SPFieldType. So here was my problem, what do you put here when is a custom field??

I solved this by altering the SchemaXml property of the field.

lField.SchemaXml = lField.SchemaXml.Replace(

"LookupMulti", "LookupNoLinkMultiField")

This ensures your custom field is used and not the standard field. Let me know if you have any other solutions for this.

 

About these ads
  1. Stuart Fieldhouse
    November 9, 2009 at 6:03 pm | #1

    Chris, could you please tell me at what point you are changing the SchemaXml to get over the LookupMulti problem? I tried putting it into the OnAdded override in the field class, but to no avail. Many thanks, Stuart.

    • November 9, 2009 at 9:01 pm | #2

      Hi Stuart,

      Well in my case I added this to a feature receiver which was stapled to a site def. When a user clicked a button I had a new sub site created which then created a bunch of libraries and lists etc. It was at this point that the feature receiver code got a handle on the look up fields and modified the SchemaXML.

      This approach is probably not appropriate for your scenario? Let me know who you intend using the field and I’ll have a think

      Chris

  2. Stuart Fieldhouse
    November 10, 2009 at 9:01 am | #3

    Thanks for the reply Chris. My field type is deployed via a WSP, so the feature receiver looked like a good route. However, I’m not assigning the type to any site columns – I’m simply trying to create a new user-creatable type which they can assign to a column in any list, just like a standard lookup column. In the receiver, since there is no actual field yet, I am looking at the SPFieldTypeDefinition and the FieldInfo but neither of these contain any schema info. When the user actually creates a column of my type, I intercept the OnAdded, but at that point the schemaXml contains only this:

    … no mention of LookupMulti. Any ideas? Thanks for your help, Stuart.

  3. Stuart Fieldhouse
    November 10, 2009 at 9:05 am | #4

    …the schemaXml I pasted in didn’t come through in the message…

    The schema contains Type, DisplayName, Required, List, ShowField, UnlimitedLengthInDocumentLibrary, ID, SourceID, StaticName, BaseRenderingType (=”Lookup”), Name, ColName and RowOrdinal

  4. January 28, 2010 at 8:47 am | #5

    You can read a workaround for this issue here (see comment from Alex Hekstra):

    WSS 3.0 SPField derrived classes
    http://willemboere.blogspot.com/2007/04/wss-30-spfield-derrived-classes.html

    The trick from Alex worked me too, thanks for that, but here is the official hotfix for the issue:

    Description of the Windows SharePoint Services 3.0 Cumulative Update Server hotfix package (Sts-x-none.msp): December 15, 2009
    http://support.microsoft.com/default.aspx/kb/977022/

    “You develop a custom field type that inherits from the SPFieldLookUp class. You want to store multiple values in a field of that custom field type. Therefore, you set the AllowMultipleValues property to True. However, after you set the AllowMultipleValues property to True, the field type is displayed as Lookup instead of the custom field type.”

    Hope that helps.

    Peter

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: