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

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

  • Comments 9

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();

        }

    }

}

 

Leave a Comment
  • Please add 8 and 8 and type the answer here:
  • Post
  • PingBack from http://msdnrss.thecoderblogs.com/2008/01/08/sharepoint-2007-mosswss-issue-with-lookup-column-to-doc-lib-name-field-3/

  • Requirement: You have created a document library and you have created another list where you wanted to

  • Can you post the feature xml files for this custom evnet handler? Thanks very much.

  • Hi Paul

    I have a little variant issue , I need to look up to a list from my document library, say i have a column in the list called document types.How can i add a this look up column to my document library.

  • Couldn't you create a workflow in Sharepoint Designer that sets FileName equal to Name and have it fire for new items?  I did that and I believe I got the same result.

  • I know one tool,very small actually, called sharepoint Document auto tittle which claims can auto set tittle to document and can be looked-up easily!

    But from your blog I fond it is not so easy to do this. Am I got it wrong or what happened?

    If you want check it visit http://www.sharepointboost.com/

    Thanks a lot!

  • Where do you put the file once it is created?

  • This code can't work for other languages because properties.ListItem["Name"] works only for column called "Name" (it's a reference to the display name and not the internal name).

    Use this line to correct it :

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

    Best regards,

    Olivier

  • Instead of using

    properties.ListItem.Update();

    use

    properties.ListItem.SystemUpdate(false);

    This method will not affect the modified (by) field and version number.

Page 1 of 1 (9 items)