PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

SP 2010: How To - Relational lists in SharePoint 2010 

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

One of the new cool things in SharePoint 2010 is the fact that we now have relational lists. In my previous article about List Joins, I talked about how you programmatically can fetch and join information from more than one list with the improved SPQuery object.

In this article I will give you an overview of what capabilities you get out of the box when installing SharePoint 2010 - in terms of relational data in lists.

Relational Lists in SharePoint 2010 - Overview

By utilizing Lookup fields in SharePoint 2010, we can enforce a relationship behavior that we previously would have to work very hard to achieve.

Microsoft have now provided us with a few new options when working with Lookup Fields:

  1. Joins between lists
  2. Projected Fields
  3. Relational integrity

Joins between lists

As mentioned, we have the capability to create relationships between lists in SharePoint 2010. This is quite easy to do using the browser UI, which I will soon demonstrate step-by-step.

Projected Fields

With projected fields we have the capability to pull information from a parent list into the view of the child list.

This basically mean that you can reference and display information from a parent list, in your child list. The fields are Read-Only but enables you to get a much nicer joined view.

Relational integrity

With SharePoint 2010 and relations in lists, you would of course wonder how it handles the relational integrity. E.g. what happens if I delete or try to delete something in the parent list?

Well, there's generally two relational integrity options:

  1. Restricted delete
    Basically the restricted delete option enables you to enforce a behavior that means that you can't delete any items that have relations from the list. E.g. if the item you're trying to delete have a bunch of child-items, you cannot delete them.
  2. Cascade delete
    Cascade delete on the other hand, means that when you're trying to delete an item which has relations - it'll delete the related items as well.

Delete and Recover related items - Recycle Bin

A question I got the other day was:

"If I delete an item in my parent list and have cascading delete so all my child items are deleted, how do I restore them if I made a mistake?"

Quite simple my dear Watson, you utilize the recycle bin. When you delete an item using Cascading Delete, the item and it's related items are placed in the Recycle Bin. From there you can obviously easily recover the items as well. This is what an item with relations looks like in the recycle bin:

[PIC]

Step by step - Do it yourself

Alright - so we've covered some of the basics of relational lists, nothing fancy. But now we want to create some lists and have relationships between them - so let's get on with it!

1. Create a parent list

  1. Create a new Custom List named "ParentList"
  2. Create a new Coumn in that list as per the following settings:
    image 
  3. Add some sample items in the list like this:
    image

2. Create a child list

  1. Create a new Custom List named "ChildList"
  2. Create a new Column in that list as per the following settings:
    image
    Please note that I checked the City checkbox. This will create a Projected Field against the lookup automatically so you can view that information which exist in the parent list - directly in the child list.

3. Test out the projected fields functionality

Add some new items in the ChildList to see that when you add an item and choose a company from the ParentList it will automatically show the projected field ("City") as a read-only field in the child-list:
image

4. What about enforcing relational behavior?

I'm glad you asked. When you create (or change settings for) a lookup field (like the "ParentLink" field), you have the ability to change settings for the relational behavior.

Go to your "ParentLink" column, or when you create a new lookup field - and see the following dialog:
image

From this dialog as you can see, you have the ability to make the necessary settings for your fields.

Restricted Delete

If you choose the "Restricted Delete" option, you will see the following behavior when trying to delete an item that has related items:
image 

Cascade Delete

If you choose the "Cascade Delete" option for your lookup field instead, you'll be sending the items directly to the Recycle Bin instead. Then it'll look like this in the recycle bin:
image

Note, that you have the icon that looks like a relational diagram in your recycle bin - this means that you've deleted an item that may have deleted linked items. If you restore this item (in our case, TOZIT) it will automatically restore all of the items that were originally deleted.

So, we have the kind of enforced relationships we've longed for since the dawn of days!

Summary

Voila. Easy as 1-2-3, you have created two very simple lists and created a relationship between them - and optionally you can enforce this relationship using the "Enforce relationship behavior" settings for Restrict Delete or Cascade Delete.

Enjoy!

 
Posted on 5-Jan-10 by Tobias Zimmergren
19 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: 2010, How To, SP2010
 

Links to this post (Trackbacks/Pingbacks)

Comments

Wednesday, 6 Jan 2010 06:52 by sudharsan
Thank you for ur post and it really helped me. But what about deleting the item through object model. Whether the same functionality will take effect or we have to do some more coding.

Wednesday, 6 Jan 2010 10:13 by Mika
thx bro. looks like ive been missing out!

Friday, 8 Jan 2010 02:36 by Suranja
thanks for this helps a lot

Tuesday, 12 Jan 2010 03:22 by David Walker
thank u for this walkthrough of relation in list. e really like the cascade options for delete

Wednesday, 3 Feb 2010 07:45 by carlo
Wow. Great article. Now, how do i get my company to use SP2010?

Tuesday, 23 Feb 2010 06:46 by Amy
Hi there, Thanks for the info. Do you know how to add the additional fields to a lookup field programmatically?

Thursday, 4 Mar 2010 04:51 by gbelzile
The problem is that there is only one level of hierarchy possible here. We can't create a lookup on a lookup column. Let's say I have 3 lists: Client, Office, Contact. Office is linked to client and contact is linked to office. I'd like to be able to show the client name in the contact list. Is there a way? Thanks,

Friday, 2 Apr 2010 04:16 by Ike
I've been trying to get the same thing working that gbelzile asked about with no luck. Also, anybody figure out how to "enforce relationship behavior" if the lookup column allows multiple values? The two options seem mutually exclusive. That stinks if relationship enforcement only works on a single item lookup.

Friday, 2 Apr 2010 05:39 by Ike
I've been trying to get the same thing working that gbelzile asked about with no luck. Also, anybody figure out how to "enforce relationship behavior" if the lookup column allows multiple values? The two options seem mutually exclusive. That stinks if relationship enforcement only works on a single item lookup.

Sunday, 18 Apr 2010 08:40 by FuadTaha
thank you for the article,

Monday, 26 Apr 2010 06:37 by Brian
Fantastic article about relational lists. Thanks for the info

Tuesday, 18 May 2010 07:36 by Andrew Stegmaier
I have run into the same difficulty as gbelzille and ike: multiple levels of lookup hierarchy.

Wednesday, 2 Jun 2010 09:07 by flash game programming
That is a wonderfully structed blog and the content is just awesome. I will get all my family soon to read your lovely posts altogether.

Monday, 14 Jun 2010 08:28 by game on
sweet tobias. or as u say: awesomme! thank u for this walk through

Wednesday, 23 Jun 2010 10:43 by Jeff Victorino
Great article showing this great improvement in Sharepoint 2010 lists. Thanks for taking the time and writing this.

Monday, 5 Jul 2010 08:49 by Praful udade
Hi, i am big fan of your all bolgs based on SharePoint 2010. All are awesome and giving very good knowledge. I would like to know how can we import data to custom list from external list automatically. There should not be any manual effort required. I created custom list with column External data, but it requires to take data manually. how to automate it.

Friday, 16 Jul 2010 01:23 by Jatinder Amar
This posting was userful.

Name:
URL:
Email:
Comments: