SharePoint List Lookup Column…!


One of my friend came to me with a question on how to create a SharePoint List Lookup column. He had gone through several approaches (Programmatically and Declaratively) and he was asking for a recommended best practice way. I remembered a bit how I did that before but was not so sure the exact steps. Then I put my hands on and was trying to do a POC and had to spend some time on it. So thought its valuable to blog it so I will have a reference back for my self. Smile

Scenario for my POC was a typical one, Department and Employee Lists. So I want a lookup field inside the Employee list so I can pick Department from a lookup column.

Step 01: Create a list that will contain data for the lookup list. In my case it will be department list and below are the properties of it

List Name = “Departments”

List URL = “Lists/Departments”

Field name that I want to lookup = “Title”

Step 02: Create a list that will hold the Employee details.

List Name = “Employees”

It already contains a field to hold the Employee Full Name “Full Name”

Step 03: Now we have to define a lookup field to show the Department names. This is the most important section because this is the place we are doing something different than for other normal list fields(columns)

<Field ID="{BE38913E-20F4-4922-8694-799501852C5E}" 
Name="Department" StaticName="Department" DisplayName="Department" 
Type="Lookup" List="Lists/Departments" ShowField="Title"/>

The list field will have all regular attributes such as ID, Name, StaticName and DisplayName. So to tell SharePoint that this new field is a Lookup field, we specify the Type attribute as “Lookup”. Then we have to specify the source list name that we will be retrieving data (Departments List). The value should be set to “List” attribute and it can be the ListId or the list path. If we are to deploy this solution in deferent locations we cannot go with a id specific column, because the ID changes, from one Site to other. So we have to specify the List Path.

One thing to note here is you need to give the full list path as it shows up in the browser URL.

image

So it should be looking like this.

List=”Lists/Departments”

Now we have a reference to the Department list and now need to decide what column should be retrieved. To specify that set the source column name to the ShowField attribute.

ShowField=”Title”

So we are retrieving the Title field from the Departments list.

That’s it Smile Add the newly created column to your list content type and in the view fields as necessary and you will be getting the lookup values as expected.

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s