Enabling File Names In Lookup Fields
For my first post, I figured that I would leverage a prototype that I worked on last week. I was investigating some of the issues that one of the Fab40 templates has after you upgrade the server from 2007 to 2010. I learned that the Knowledge Base template has a novel way of solving one of the problems that lookup fields have when using a document library.
Before we dig into the solution to the problem, I should first spend some time describing the problem.
When you create a lookup field, you need to choose a field from the “other” list to be used as the display value. When you are editing an item from “this” list, you will be shown a drop-down list box that allows you to choose an item from the “other” list. When each of the items from the other list are added to the drop-down list, some text value from the other item needs to be displayed here that uniquely identifies the item. Some fields work better than other fields.
For example, let’s say that you are adding a lookup field that is going to be used to select someone from a list of contacts. Using the Zip Code as the field that is displayed in the drop-down list isn’t likely to be very helpful. It would be extremely common for you to have multiple contacts that are in the same zip code. You would then see the same zip code listed more than once in the drop-down list and you would have no idea which entry represents which contact. Choose a contact would be a pretty frustrating experience. Using a field like Name would be a lot better. Granted, the Name field could also contain duplicates. But it would still be a whole lot better than a field like Zip Code which is likely to contain many duplicates.
The problem we have is when the “other” list is a document library. Which field should you choose for the lookup field to display? You could use the Title field but it doesn’t always get populated. Upload some text files into a document library and they will all wind up with an empty value for the Title field. Even if a Title value is specified for each document, this field allows duplicates which, as described above, is not good. So, is there anything about the documents in the document library that is normally going to be unique? Well, the file name. Duh! However, try creating a lookup field to a document library and you’ll notice that there is no file name field to choose. Doh!
The file name is exposed through the object model as a field named FileLeafRef. So why isn't this field available to use in lookup fields? I’m still trying to find someone that can explain that to me. If I figure it out I’ll update this post.
Ok, so now we can (finally) get back to the Knowledge Base site template that I was looking into and how it solved this problem.
One of the features that the Knowledge Base site template has is that articles written in the wiki library can each have one or more related articles. This was accomplished using a multi-value lookup field. Now, multi-valued lookup fields don’t exactly have the greatest UI experience. But they do allow the KB article author to accomplish the scenario without the KB site template developer having to invest in custom (read: expensive) list item selection UI.
But if we're using a multi-value lookup field to choose a document from a document library, what was the field that was used as the lookup field? Well, what the KB site template did was to add a text field to the document library called “Name” in conjunction with an event handler that would ensure that the value of this field always contained the actual name of the file. As wiki articles were created or modified, the event handler would always ensure that the Name field contained the right value.
Now, the way that the KB site template did this was specific to this site template and isn’t really reusable in other site templates. The concept is reusable but the specific implementation in that site template isn’t. So I created a sandboxed solution that contains a reusable implementation of this concept.
Using this solution is pretty easy:
- Upload the solution - Upload the solution to the Solution Gallery in your site collection and activate it.
- Activate the site feature - In each of the sites where you want to use this File Name field, activate the site feature named “BillGr's File Name Field”.
- Add the site column - In each document library where you want to use this field, add the site column named File Name to either the document library or the content types that are used in the document library.
Solution Implementation
In this solution, you’ll find three things:
- A site column that defines the File Name field.
- An assembly that contains a sandboxed event handler.
- A feature (which can be activated/deactivated) with two element manifests:
- One that defines the File Name field.
- One that wires-up the event handler to the ItemAdding and ItemUpdating events.
“File Name” Site Column Definition
One thing to note about the site column is that it has been defined with a very unique internal name (the Name attribute). This is to deal with field name collisions. What if a user has already defined a File Name column? What if they already have other data in that column? What if they already have business logic that is driven by the value of that field. My event handler can’t simply look for the first field named “File Name” that it finds and blindly overwrite its value with the current file name. This is one of the reasons why a field definition has both an internal name and a display name. As an aside, the other main reason for internal names is so that compiled code doesn’t need to be changed when the name of a field is localized into another language. But that’s a subject for a different post.
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
- <!-- NOTE: The Name attribute here must match the value of the FileNameFieldInternalName property
- of the ListItemEvents class that is in ListItemEvents\ListItemEvents.cs -->
- <Field ID="{1511BF28-A787-4061-B2E1-71F64CC93FD5}"
- Name="BillGrFileNameField_FileName"
- DisplayName="File Name"
- Type="Text"
- Required="FALSE"
- Group="Custom Columns">
- </Field>
- </Elements>
In this case, we’ve given our field an internal name that uses the solution name as a prefix. It is highly unlikely that another solution will define another field with this same internal name. Later, our event handler will use the same internal name when accessing the field value from the list item.
Event Handler Wire-Up
When a feature definition includes an event handler, usually the event handler has a ListTemplateId or ListUrl attribute. These attributes are normally used to narrow down the scope of the event handler to a specific list type. Since I haven’t included any of these attributes, my event handler will be globally registered. It will be called whenever any item in a list or document library is added or changed – anywhere in the site.
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
- <Receivers>
- <Receiver>
- <Name>ListItemEventsItemAdding</Name>
- <Type>ItemAdding</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>BillGr.Samples.FileNameField.ListItemEvents</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- <Receiver>
- <Name>ListItemEventsItemUpdating</Name>
- <Type>ItemUpdating</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>BillGr.Samples.FileNameField.ListItemEvents</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- </Receivers>
- </Elements>
Event Handler Implementation
The event handler simply ensures that the value of the File Name field is correct given the current name of the document. The code relatively simple. Whenever we’ve been notified that an item has changed, we first have to confirm that the item is in a document library. If it is a “regular” list item, then it won't have a file so there isn’t any file name to stay in sync with. Now, please, no arguments here about Attachments <grin />. SharePoint treats the primary file stream of a document library item very differently from an attachment to a list item.
In addition to confirming that the item is in a document library, the event handler also needs to confirm that the item has a “File Name” field. If it doesn’t have the field then, again, there isn’t anything that we need to do.
Once we’ve ruled out these other scenarios, all that remains is to extract the file name from the URL and then ensure that the value of the “File Name” field contains this same file name.
- using System;
- using System.IO;
- using System.Security.Permissions;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Security;
- using Microsoft.SharePoint.Utilities;
- using Microsoft.SharePoint.Workflow;
- namespace BillGr.Samples.FileNameField
- {
- /// <summary>
- /// List Item Events
- /// </summary>
- public class ListItemEvents : SPItemEventReceiver
- {
- /// <summary>
- /// Gets a string containing the internal name of the "File Name" field.
- /// </summary>
- public static string FileNameFieldInternalName
- {
- get
- {
- // NOTE: The value used here has to match the value of the Name attribute in the field
- // definition that is in FileNameSharedField\Elements.xml.
- return "BillGrFileNameField_FileName"; // Ensure that this is globally unique by using a prefix that is not likely to be used by someone else.
- }
- }
- /// <summary>
- /// An item is being added.
- /// </summary>
- /// <param name="properties">An SPItemEventProperties object that represents properties of the event.</param>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- ListItemEvents.UpdateFileNameField(properties);
- properties.Status = SPEventReceiverStatus.Continue;
- }
- /// <summary>
- /// An item is being updated.
- /// </summary>
- /// <param name="properties">An SPItemEventProperties object that represents properties of the event.</param>
- public override void ItemUpdating(SPItemEventProperties properties)
- {
- ListItemEvents.UpdateFileNameField(properties);
- properties.Status = SPEventReceiverStatus.Continue;
- }
- /// <summary>
- /// Helper method that ensures that the "File Name" field contains the correct value for the specified list item.
- /// </summary>
- /// <param name="properties">An SPItemEventProperties object that represents properties of the event.</param>
- private static void UpdateFileNameField(SPItemEventProperties properties)
- {
- // We can only do this if the item is in some form of a doc-lib and the content type of
- // the item includes the "File Name" field.
- if ((properties.ListItem == null) ||
- (properties.ListItem.File == null) ||
- (!properties.ListItem.Fields.ContainsField(ListItemEvents.FileNameFieldInternalName)))
- {
- return;
- }
- // Extract just the file name from the server-relative URL. Note: URLs with forward slashes
- // are compatible with the System.IO.Path class. We are using that here so that we don't
- // have to do any URL parsing. Any time that we try and do URL parsing on our own we
- // eventually get into trouble beause of some corner case that wasn't accounted for in
- // the parsing code. We'll avoid that problem if we leverage the Path class which has
- // already been extensively tested.
- string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(properties.AfterUrl);
- // Ensure that the "File Name" field contains the current file name.
- properties.AfterProperties[ListItemEvents.FileNameFieldInternalName] = fileNameWithoutExtension;
- }
- } // class ListItemEvents
- }
Downloading The Sample
There are two downloads available from the MSDN Code Gallery for this sample:
- SharePoint Solution - This is a working .WSP that you can upload to the Solution Gallery for your site collection and activate it. Once you do this, the “BillGr's File Name Field” feature will be available to activate in all sites of your site collection. After activating the feature, you can add the site column named “File Name” to a document library to start getting updated file names. You'll find this site column in the “Custom Columns” group.
- Visual Studio Project - This is the VS 2010 SharePoint solution that was used to generate the solution above.
As with any of my other samples, you are free to use the solution in your site or take the source code and leverage it in some other solution that you are working on. Just keep in mind that this is a sample and, as such, has no warranty, no guarantee that it will work in all environments, and no promise that a future version of SharePoint won't break it.
Comments
- Anonymous
November 08, 2010
Is this WSP for SharePoint 2007 or SharePoint 2010? - Anonymous
February 01, 2011
The concept works on either SharePoint 2007 or SharePoint 2010. The sample that you can download is a sandboxed solution for use with SharePoint 2010. - Anonymous
January 13, 2013
can you get the filename to display in a column for a task list and not a document library? Ex. A list item has an attachment and you want a column to display the name of the attachment? (working with sharepoint 2007)?