Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

Performing joins between SharePoint lists

Posted on 10/20/2007 @ 3:44 PM in #Sharepoint | 45 comments | 20411 views

Yeah you read that right. They told you it couldn't be done - I'm tellin' ya it can be done.

Here is how.

First, go ahead and create two lists

a) Customers (Title)
b) Orders (Title, and a lookup value called 'Customer' - which, you guessed it right, is a lookup on the Customer.Title SPColumn).

Here are my lists -

       and     

Great - now, this can be done on any ASPX, but to be clean and to the point, open the site in SharePoint Designer, and add a new ASPX - Untitled_1.aspx (that's the default name).

1. In SPD, go to Data View --> Manage Data Sources

2. In the window that appears on the right, create a new Linked source as shown below:

3. Click on "Configure Linked Source" and go ahead and add both "Customers" and "Orders" and click "NEXT"

4. Make sure that you choose to Join the contents of the two data sources as shown below -

5. Give it a meaningful name such as "Customer-Orders" in the "General" tab.

6. Great, now while in the <form> tag in your ASPX, click on Data View --> Insert Data View, your UI should now say "Current Data Source - Customer - Orders" - "next insert fields to create a view".

7. Now from the "Data Source Details" pane, select Customers.Title. and choose - Insert Selected Field as "Multiple Item View" as shown below:

Your ASPX should look like this -

8. Now place the cursor in front a particular value - Say "Scot Hillier", and now in the Data Source Details Pane, Select Orders --> Title, and choose to insert as "Joined SubView" in the menu shown below:

Specify Join information as shown below:

 

9.   Thats it! Format it a little bit, run the ASPX - looks like this -

Thus, as you can clearly see from my blogpost - Andrew is buying Corvette's and Mansions, while Scot and myself are buying basic necessities of life.


On 10/22/2007 8:45:25 PM Steve Schapel said ..
I only know of one Sahil Malik, so that's safe. But I know two people named Andrew Connell (true story!). Can this trick be done by joining on the basis of a numerical ID field, rather than using the person's name?

On 10/22/2007 10:04:00 PM Sahil Malik said ..
Hey Steve - Yes you can use IDs as well. But I bet Andrew will still be buying Mansions and Mercedeses!! Sahil

On 10/28/2007 6:29:01 PM Mark said ..
This is cool! Do you know if it's possible to aggregate values from the subview; for example to display only the number of orders for each customer? Mark

On 11/19/2007 3:45:30 PM Vivian said ..
Great. I have same question as Mark. I see that it is done at dashboard.aspx at the budget and trace for multiple project template to get the count from the subview. I cannot figure it out how it is done. Do you have any idea?

On 12/2/2007 4:43:48 PM John Haigh said ..
Hi, Do you know if there is a way to filter the data in the aggregated datasource..i.e. by a query string value (ID=2) whereby you would only show a Customer with the ID of 1? I have tried but have been unable to pass filter values to the aggregated datasource. Thanks, John Haigh

On 12/4/2007 10:46:29 AM MARCOS ROJAS said ..
PLEASE I NEED HELP ALWAYS PROGRAME IN VB5, AND NOT WANT TO CHANGE THE VERSION. BUY NOW AN LATOP THAT HAVE WINDOWS VISTA, INCOME AND WHEN A "COMPONENTS" I DO AND CLICK ON "MICROSOFT FORM 2.0 OBJECT LIBRARY" CLICK AND DO IN OK ME HE DENIES PERMISSION. I DO THAT?

On 12/5/2007 11:48:36 AM Eric Sammann said ..
That is a great feature. I actually discovered that a couple of months ago, but now I need to do a join to accomplish a lookup from one table in order ot update another table. If you go to Data View -> Insert Data View, then you get several options when showing data from one table. the options you get are: Single Item View Multiple Item View Single Item Form Multiple Item Form New Item Form if you do the same thing on a joined datasource then you only get the first two, no form options. Do you know if something like that is possible?

On 12/7/2007 6:29:11 PM maria said ..
What is the benefit to joining the two lists when you can achieve the same results by just creating a lookup from one list to another, grouping the customer name and then selecting a total to display?

On 12/17/2007 11:28:13 AM John said ..
Great post. I now have the aspx file that I can browse from my sharepoint site. Is there a way to add it as a webpart? Maybe I could have three webparts, the customer list, the order list and then the aggregate (customer-order) as a third view in a custom page with 3 columns. I'm off now to view you dnr tv sharepoint shows!

On 12/17/2007 11:30:53 AM Sahil Malik said ..
John - It *is* already a webpart. :) If you are asking, if this can now be packaged, and dropped via a browser. The answer to that unfortunately is No :(. It has to be a sharepoint designer thing. SM

On 12/19/2007 1:34:49 AM Steve said ..
Thanks for your great share. May I ask if I could post this joined data as one of my customized views on MOSS site. Or I just need to package it as a web part and insert it on MOSS site.

On 12/20/2007 2:54:26 PM Steve Sherman said ..
I'm following your instructions word for word, but the graphic representations are not showing. I would like to be able to see those as I think they will help in concluding the project I have going. Any way I can get those? Thank You,
Steve Sherman

On 12/20/2007 8:13:18 PM Bill said ..
I've created a view with a joined subview as described. It works great. I do have a problem applying conditional formatting though. I am using an html view style available in SPD (second selection). I can set up the filter conditions for the formatting (I am showing or hiding a pic based on a field value), but the pic just disappears (wether it is supposed to or not). I tried applying font and cell background changes but they don't work either. Any ideas that might help me? Thanks

On 1/10/2008 7:20:15 AM Antonio Calvo said ..
Thank you very much for the post. Im using this tip to show a table with results from two joined list, for example "project" and "customer". One of my lists ("project") contains a lookup field to the other one ("Customer"). In order to avoid integrity problems I have configured that column to use de ID of the item instead of, for example the title, as you do in your post. That solves the problem but it generates another one: you have to develop custom forms for each list. I mean, you cannot present the user the default EditForm.aspx for the list "project" because the dropdown control to select the corresponding customer will show the list of IDs. As you can imagine it will be great to show here the "Title" field from the customer (or even something like ID-Title"). ¿Can anyone point me in the right direction to solve this problem with the minimun work? Actually is it any way of acomplish this using two webparts on the form connect somehow? Im a bit lost trying diferent things so, any tip you could provide will be a great help. Thank you!

On 1/16/2008 11:10:18 AM Matt said ..
Is there a way of using the context menu (View Properties, Edit Properties, Check out, Workflow etc) like seen in standard document libraries for each row? I can't seem to get this working with a Data View web part (without converting from a Lits View web part to a Data View web part, but when I add another data source it stops working).

On 1/16/2008 11:45:09 AM Sebastian Soanca said ..
Hi Malik,
Nice post. I have the same situation but i want to use the join feature in Add New Item/Edit Item mode. It is possible to linked 2 look up list and filter the child list after item selection in parent list ? Thank you.

On 1/17/2008 9:11:08 PM Leren said ..
good post. I got it working immediately

On 1/28/2008 8:40:36 AM Michael said ..
I am trying to perform this operation but everytime I try to access the data (show data)I receive the following message:
You do not have permission to do this operation. Ask your web site administrator to change your permissions and then try again, or log on with a user account that has this permission.
My server admin states: "There was a service pack released a very good while ago that caused the data view web par, and some otherst to not work for administrators of sharepoint sites as it should. I opened a ticket with MS on this, they confirmed it was a bug, were able to recreate the scenario in their lab, and said they were addressing it, right along with the “we don’t know when the hotfix will be out” statement." I am able to set up sharing data between lists through the sharepoint site interface and a lookup field will pull data from the other list but I am unable to get the data related to the lookup field to pass. What field should I be setting up to display this information? A text box? In other words if I select a customer in the selection field, I want other fields to populate automatically with that customers info. We are on server 2003. Any ideas?
Thank you,
Michael

On 2/11/2008 7:04:00 AM Adam said ..
I'm accustomed to joining tables in a query via SQL Server and Access and am looking to replicate the results of a simple SELECT statement, whereby the results are returned in a simple tabular form. Is it possible to replicate this via SharePoint?

On 2/11/2008 1:09:24 PM Sahil Malik said ..
Adam - Yes you can. Look into SPQuery and CAML.

On 2/14/2008 2:37:36 AM Nitin said ..
Hi Sebastian, Showing data in Parent/Child Mode is possible if you use ListViewWebParts and then link them. I have done it and it works well. Thanks,
Nitin

On 2/28/2008 9:23:23 AM Tritata said ..
Hi.
For example, there're two lists, one of them has a lookup field referenced to the other list's field. Is there way to retrieve information from two lists by one query? Something like this SQL statment does: "SELECT List1.field1,... List1.fieldN, List2.field1,... List2.fieldM FROM List1, List2 WHERE List1.Lookup=List2.Id".

On 3/1/2008 6:42:13 AM Dean said ..
Salik, this is very helpful, do you know how to take this a step further by having lookup fields in a list/library that are related? e.g., After the State field is choosen a subset of Cities is available to choose from when entering metadata for a document

On 3/10/2008 4:09:48 PM Tomasz Szalaj said ..
Is it possible to make some example of using controls from Ajax Control Kit with Sharepoint Webservices to create cascading filters for lookups to Sharepoint lists? Greetz!
TSz
http://www.it-dev.pl

On 3/11/2008 6:17:53 PM Thia said ..
Just what I have been looking for - only When I click "Joined Subview" instead of giving me the Join Subview menu, it just ads the information from the second list in front of the information from the second list - onthe same line. How do I get the Join Subview Menu to pop up? Thanks!

On 3/12/2008 10:18:15 AM Thia said ..
OK - I was able to get the "Joined Subview" to come up sometimes, but even when it came up, it was only on 2 lists. What if I want to join 3 lists? Such as: List A
List B
List C Where List C has a lookup to List B and List B has a lookup to List A.

On 3/27/2008 6:14:26 PM doctor research said ..
Hi, Do you know if there is a way to filter the data in the aggregated datasource..i.e. by a query string value (ID=2) whereby you would only show a Customer with the ID of 1? I have tried but have been unable to pass filter values to the aggregated datasource. Thanks, John Haigh

On 4/4/2008 3:03:57 PM Xurgum said ..
Hi, Thanks for an awesome post Sahil. Do you know if this would also work for lookup fields with multiple selection? E.g. what if customer column in Orders list could hold both Scott and Andrew for the order of banana in the same record. (hey Andrew could also have a banana =) Xurgum

On 4/5/2008 6:10:09 AM girlgeek said ..
Can I create an editable form with joined data? I want to create a editform.aspx that allows me to provide a way for the user to provide child items to a parent item (somehow grabbing the ID of the parent for the user). thank you for posts like these.

On 4/27/2008 11:40:20 PM Alejandro Lebrero said ..
Can do the same but using two lists placed into different sites?

On 4/28/2008 4:06:35 AM drTheory said ..
I'll repeat the question Tritata wrote... Did anyone managed to join tu lists in to one as we're able to do with SQL statements? Something like Tritata already wrote: "SELECT List1.field1,... List1.fieldN, List2.field1,... List2.fieldM FROM List1, List2 WHERE List1.Lookup=List2.Id".

On 5/2/2008 4:36:03 AM Zuber Agwan said ..
this is a great post i just wanted to ask that i want this to be done in the sps 2003 site using spd 2007
but i m not able to get the "create a linked source" hyperlink when i open sps 2003 site in spd 2007
sujjest something ????

On 5/23/2008 1:40:57 PM akash said ..
hi,
I used the same approach, only thing is i had "thumbnail"column of a picture column as a second list, which i wanted to use.....
now when i join the thumbnail dosent show up,,instead it gives a numeric value.....
why is that and what is the work around?

On 5/28/2008 12:40:54 PM tgsims said ..
If I understand your solution, you are sending the join to a SharePoint page (URL). In a sense this is just a virtual join. How do I send the join to a new or existing SharePoint list?

On 6/8/2008 9:37:27 PM Jeremy said ..
I got lost at step 6. [Great, now while in the <form> tag in your ASPX, click on Data View --> Insert Data View, your UI should now say "Current Data Source - Customer - Orders" - "next insert fields to create a view".] I don't know what you mean by "in the <form> tag of you ASPX". When I Data Source Detials tab I have my Customers - Orders as the Current Data Source and I have but the "Insert selected fields as..." button is greyed out. What am I doing wrong? Am I supposed to have something in particular displayed the main part of the SPD gui? Sorry I am not hip to your nomenclature, but "my ASPX" is referring to linked sources that I just made in steps 3-5?

On 6/22/2008 8:35:43 PM pradeep said ..
Hi, My developers have been trying to do this for months... but with your article it just took 10 mins to have this up and running. very valuable indeed. thanks.

On 6/22/2008 11:34:21 PM Sahil Malik said ..
Ok - you owe me Chai Samosa then!

On 7/23/2008 4:27:12 PM JulieAllyn said ..
How do you get the formatting results as displayed? The customer name appears at the bottom of each grouping for me. How to place it at the top? ALSO: How to display the label Orders just once in each grouping?

On 7/23/2008 7:58:45 PM André Rentes said ..
Hi Can I join a sharepoint list and a database table!? Do you have samples? Thanks!

On 8/19/2008 4:22:11 PM Fabio said ..
Hi, can we use the ID of an lookup field instead of the value?? Because, there are some special characters that does not work with your example. If you replace "Andrew Connell" by "Johnson & Johnson", it says that could not find any order for this customer... the problem is the "&" character. Any ideas? Thanks,
Fabio

On 8/26/2008 4:40:35 AM KALASH said ..
see the tables below.
I want to create db schema in lists. means lists should be linked to each other as RDBMS work. can i use the lists instead of rdbms db. actually i dont want to use db. these lists will hold thousands of records and i also want to perform re-search and Tracking on created lists.
------------------------------------------------------------------------------------------ USE [parameter_DB]
GO
/****** Object: Table [dbo].[search_details] Script Date: 08/26/2008 14:34:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[search_details](
[order_search_detail_id] [bigint] IDENTITY(1,1) NOT NULL,
[search_id] [bigint] NOT NULL,
[county_field_id] [bigint] NOT NULL,
[timestamp] [datetime] NOT NULL CONSTRAINT [DF_search_details_timestamp] DEFAULT (getdate()),
[Value] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[record_order_flag] [bit] NULL,
CONSTRAINT [PK_order_feature_junction] PRIMARY KEY CLUSTERED
(
[order_search_detail_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
USE [parameter_DB]
GO
ALTER TABLE [dbo].[search_details] WITH CHECK ADD CONSTRAINT [FK_order_feature_junction_county_field] FOREIGN KEY([county_field_id])
REFERENCES [dbo].[county_field] ([county_field_id])
GO
ALTER TABLE [dbo].[search_details] WITH NOCHECK ADD CONSTRAINT [FK_order_feature_junction_order] FOREIGN KEY([search_id])
REFERENCES [dbo].[search_history] ([search_id])
GO
ALTER TABLE [dbo].[search_details] CHECK CONSTRAINT [FK_order_feature_junction_order] USE [parameter_DB]
GO
/****** Object: Table [dbo].[search_history] Script Date: 08/26/2008 14:35:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[search_history](
[search_id] [bigint] IDENTITY(1,1) NOT NULL,
[orderTimestamp] [datetime] NOT NULL CONSTRAINT [DF_search_history_orderTimestamp] DEFAULT (getdate()),
[user_code] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[company_id] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[county_fips] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[state_fips] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[station_id] [varchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[outputtype] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[order_type_flag] [tinyint] NOT NULL,
[order_name_value] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[title_officer_value] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[order_comment_value] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[keep_order_open_value] [bit] NULL,
CONSTRAINT [PK_order] PRIMARY KEY CLUSTERED
(
[search_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
USE [parameter_DB]
GO
ALTER TABLE [dbo].[search_history] WITH NOCHECK ADD CONSTRAINT [FK_order_order_type_flag] FOREIGN KEY([order_type_flag])
REFERENCES [dbo].[search_type_flag] ([order_type_flag])
GO
ALTER TABLE [dbo].[search_history] CHECK CONSTRAINT [FK_order_order_type_flag]
GO
ALTER TABLE [dbo].[search_history] WITH CHECK ADD CONSTRAINT [FK_order_user] FOREIGN KEY([user_code])
REFERENCES [dbo].[user] ([user_code])
------------------------------------------------------------------------------------

On 8/28/2008 2:41:33 PM Sunder said ..
We have a similar situation as Kalash. We would like to use Sharepoint lists as database tables. This has been going well for us but we have run into a situation where we would like to implement a conditional lookup. For example in a list if a person selects a value from a lookup field (drop down), we would like to display related values from the list the lookup field is refering to. Would this be possible in Sharepoint? Any help would be appreciated.

On 9/15/2008 9:38:10 AM Rasmus Bodin Löfgren said ..
Thanks for a great post! You can achieve more join-like nature by passing the parent to the child rows. In this example it would allow us to show Customer fields on the Orders row. <xsl:template name="dvt_2.body">
<xsl:param name="Rows" />
<xsl:param name="dvt_ParentRow" /> <xsl:for-each select="$Rows">
<xsl:call-template name="dvt_2.rowview">
<xsl:with-param name="dvt_ParentRow" select="$dvt_ParentRow" />
</xsl:call-template>
</xsl:for-each>
</xsl:template> <xsl:template name="dvt_2.rowview">
<xsl:param name="dvt_ParentRow" />
<tr>
<td>
<xsl:value-of select="@Customer" />
<xsl:value-of select="$dvt_ParentRow/@Title" />
</td>
</tr>
</xsl:template>

On 10/6/2008 10:38:22 PM Anthony said ..
Hi, I had the same approach as akash and still trying to show some picture with the ImageThumbnailDisplay Column. it only give a 0 as result. do you know how to make it work?

On 10/7/2008 3:38:47 PM Eric Damon said ..
Hi Nitin,
You said "Showing data in Parent/Child Mode is possible if you use ListViewWebParts and then link them." Can you elaborate on this? I have the exact same need as Sebastian Soanca.

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.