Archive

Posts Tagged ‘Custom Lookup Field’

Custom Lookup Fields

March 15, 2009 Chris Forbes 5 comments

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.