search blog
most popular
MCP MCTS MCT MVP

SPS 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
8 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,

Name:
URL:
Email:
Comments: