4
votes

How do you create a lookup column to a Document Library that uses the 'Name' of the document as the lookup value?

I found a blog post that recommends adding another custom field like "FileName" and then using a item reciever to populate the custom field with the value from the Name field but that seems cheesy. Link to the blog in case people are interested: http://blogs.msdn.com/pranab/archive/2008/01/08/sharepoint-2007-moss-wss-issue-with-lookup-column-to-doc-lib-name-field.aspx

I've got a bunch of custom document content types that I dont want to clutter with a work around that should really work anyway.

5

5 Answers

4
votes

I created a one step workflow to set the title from the name, fired on modify and created. Seems to work and took seconds to create.

3
votes

One way you can do this (although not the easiest way) is by creating a custom field type that extends the SPFieldLookup class. SharePoint's field editor for Lookup fields purposefully hides any columns types that aren't supported by Lookup fields, but you can create a field editor for your custom field type that shows them.

However, I have created a Lookup column that points to a Name column in a Document Library before, and it probably doesn't work like you'd expect. While the value stored in the lookup column is valid, it doesn't show up right in List view or on the View Properties form.

The solution you posted may actually be the best way to handle this. Lookup fields require some kludges if you want to handle more complex scenarios, but that's because they're not meant to provide the same functionality as a foreign key relationship in a database.

1
votes

Coding in any form always scares me. So Here's what I did: I simply renamed the Stupid "Title" Field to something else, say "Keywords", since you cant do anything with that field: cant even make it mandatory. Then I created another Single line field called "Title" and used this field for the Lookups

0
votes

Well there is a simple solution to that and in might work in some case. In the nutshell if you make the Title field Mandatory, this will force the user to enter a title. In that manner we can use title field as a lookup field.

Now How to do that? One you are done create a document library go to the library setting. Select Advance Setting and Select Yes for the option "Allow management of content types?". Then go back to the Library setting and Under content types select the "Document" Content type. THen Select Title Column and then Select "Required (Must contain information)" and say OK.

Now try uploading a document to this document library. You will see Title field in the form.

Hope this helps Cheers Vaqar

0
votes

You have to add the field as XML with the ShowField as 'FileLeafRef'

var XmlFieldDefinition = "<Field DisplayName='myLookupColumn' Type='LookupMulti' StaticName='myLookupColumn' Name='myLookupColumn' Required='FALSE' List='THE LOOKUP ID HERE' WebId='THE WEB ID HERE' UnlimitedLengthInDocumentLibrary='TRUE' Mult='TRUE' Sortable='FALSE' ShowField='FileLeafRef' />"

Field fld = fieldCollection.AddFieldAsXml(XmlFieldDefinition, true, AddFieldOptions.DefaultValue);
ClientContext.Load(fld);

ClientContext.ExecuteQuery();