SharePoint 2007 (MOSS/WSS) Issue with lookup column to Doc Lib Name Field

Requirement:

You have created a document library and you have created another list where you wanted to have a lookup type column and wanted to use that lookup column to receive the Name of the documents from the doc lib. But you cannot get the Name field in the drop down for lookup column.

 

Apparently this is a limitation and there is no direct resolution out of the box.

 

Anyway, I found a workaround for this and it involves coding. I have created a sample code for a custom Item Event receiver feature. I have created a new field in the doc lib called FileName. Now after activating this feature, once we add any document to the doc lib the value of the Name field will be copied to FileName field. Now this FileName field is available in the lookup dropdown which solves our purpose. Here is the code for the custom feature:

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace NameLUpHandler

{

    public class Processor : SPItemEventReceiver

    {

        public override void ItemAdded(SPItemEventProperties properties)

        {

            if (properties.ListItem.Fields.ContainsField("FileName"))

                properties.ListItem["FileName"] = properties.ListItem["Name"];

            properties.ListItem.Update();

        }

    }

}