Raghu's SharePoint Corner: Create list with lookup column assosciated with the same list or other list.

My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://www.sharepointcolumn.com
and update your bookmarks.

September 15, 2008

Create list with lookup column assosciated with the same list or other list.

I had run into a scenario where I need to have a lookup column in the same list and many views which are based on the look up column, at first I thought this is simple to implement, but once I started implementing it, I realized that it’s not so simple. I googled on net to find an alternate for my solution, but the soultions didn’t work for me.

Before entering the implementation part I would like to disccuss about lookup column archintecutre in list. Lookup column has two important attribute lookup list and the lookup field.

Lookup List: Indicates to which list the lookup filed should refer.
Lookup Field: Which column or filed need to be refferd or showed from the list specified above.

As a regular methodology I started implementing the list using feature since it would be easy to maintain views, I created a list using feature, b ut the prob was the lookup comun was not showing up any data. Later I came to know that internally look up column uses list id to keep the track of which list it should reffer. So I googled to find out how we can update the list id of a lookup column. You cannot update the lookup column using OOB UI if you have created a list using feature or list template. I went through the following blogs

1. http://www.sharepoint-tips.com/2007/04/fixing-lookup-fields-in-list-definition.html

2. https://www2.blogger.com/comment.g?blogID=20371103&postID=4251369915225974613

Since the lookup column requires List ID, there was no point in creating list using feature because the list id will be known only when the list is created.

I thought of another alternate is to create a list using feature and than to delete and create lookup column again by programmatically, but by doing this all my views related to the lookup column failed and when I tried to create a new item in the list, all the column which were created were not visible in the newform.aspx.

Finally I came up with a solution, which has a nice amount of coding effort. The solution was to create a list, lookup column and views programmatically (using code).

Here’s the code to fix the solution of Lookup Column in the same list.
SPList list = web.Lists[listName];
list.Fields.AddLookup("ParentMenu", list.ID, false);
SPFieldLookup parentMenu = (SPFieldLookup)list.Fields.GetField("ParentMenu");
parentMenu.LookupField = "MenuName";
parentMenu.Update();
list.Update();

Here’s the code to fix the solution of Lookup Column in other list.
SPList list = web.Lists[listName];
SPList newlist = web.Lists[newlistName];
list.Fields.AddLookup("ParentMenu", newlist.ID, false);
SPFieldLookup parentMenu = (SPFieldLookup)list.Fields.GetField("ParentMenu");
parentMenu.LookupField = "ColumnInNewList";
parentMenu.Update();
list.Update();

This is a work around and a long approach, feel free to respond or comment if u have any other alternate or better solution.

No comments: